I have a list (mylist) of data frames named a, b, and c. I want to apply a simple addition to every value of the first variable (var1) of each data frame, corresponding to (matched) values from a separate data frame (shift_df), which contains the variables "id" (b, a, c) and "shift" (2.1, 4.5, 6.6). In other words I want to get a list that has every value in mylist$b$var1 added by 2.1 etc.
mylist <- list("a" = data.frame("var1" = c(1,2,3), "var2" = c(9,3,5)), "b" = data.frame("var1"= c(2,4,7), "var2" = c(5,1,5)), "c" = data.frame("var1" = c(8,5,6), "var2" = c(5,3,3)))
shift_df <- data.frame("id"=c("b", "a", "c"), "shift"=c(2.1, 4.2, 6.6))
My idea was to use lapply for this, but I'm not able to apply the matching with shift_df$id properly. Grateful for any help!
lapply(mylist,function(x) {x[names(mylist)==shift_df[1]] + shift_df$shift[names(mylist)==shift_df[1]]})