Variations of this question have been answered, but I am struggling to apply them to my dataset as someone relatively inexperienced with R/programming. I have several data frames and I am trying to use a for loop to create a new variable added to each of the data frames.
Each data frame has the exact same column names, but the data frames represent different time points (e.g., w1 is wave 1, w2 is wave 2, etc.).
I completed a for-loop to 1) reverse score several items, then 2) compute a new variable which is just a sum of several variables.
w1
cesd1 cesd2
0 0
1 1
2 1
w2
cesd1 cesd2
3 0
1 2
2 1
waves = list(w1, w2)
for(i in 1:length(waves)) {
waves[[i]]$cesd2R <- abs(waves[[i]]$cesd2 - 3)
waves[[i]]$cesdTot <- with(waves[[i]], cesd1+ cesd2R, na.rm = T)
}
I would like to get an output that includes all of the previous columns in each data frame PLUS the new columns I compute in the loop ("cesd2R" and "cesdTot") in each of the data frames.
w1
cesd1 cesd2 cesd2R cesdTot
0 0 3 3
1 1 2 3
w2
cesd1 cesd2 cesd2R cesdTot
3 0 3 6
1 2 1 2
When I run the loop, I get no errors but nothing seems to happen. What am I missing? Thank you very much!