I am trying to create a new conditional column based on two conditionals. I want to find the mean of columns A, B, and C based on the name and weeks leading UP TO (but not including) the week in the corresponding row. Let us take Joe as an example. For row 1, we would have no data in the new columns. For row 2 would have the "mean" of week 1 data. for Row 3, we want the mean of week 1 and week 2 data. In practice, we can have more weeks than 3, but I wanted to simplify the example. I would like to avoid loops if possible.
This is fairly easy to do in Excel with the Averageifs() function but I want to automate/consolidate the process with R
my data looks something like this:
Name Week A B C
Joe 1 5 6 7
Joe 2 4 5 6
Joe 3 2 3 4
Tim 1 7 8 9
Tim 2 5 4 6
Tim 4 3 5 4
Bob 1 9 8 7
Bob 3 8 5 2
Bob 4 4 5 3
The new data would look something like this:
Name Week A B C A_2 B_2 C_2
Joe 1 5 6 7 NA NA NA
Joe 2 4 5 6 5 6 7
Joe 3 2 3 4 4.5 5.5 6.5
Tim 1 7 8 9 NA NA NA
Tim 2 5 4 6 7 8 9
Tim 4 3 5 4 6 6 7.5
Bob 1 9 8 7 NA NA NA
Bob 3 8 5 2 9 8 7
Bob 4 4 5 3 8.5 6.5 4.5
Thank you for any help you can provide! I am semi-new to R and have been struggling with this problem