In df1, I need to replace the values ββfor msec with the corresponding values ββin df2.
df1 <- data.frame(ID=c('rs', 'rs', 'rs', 'tr','tr','tr'), cond=c(1,1,2,1,1,2), block=c(2,2,4,2,2,4), correct=c(1,0,1,1,1,0), msec=c(456,678,756,654,625,645)) df2 <- data.frame(ID=c('rs', 'rs', 'tr','tr'), cond=c(1,2,1,2), block=c(2,4,2,4), mean=c(545,664,703,765))
In df1, if correct==0 , then specify df2 with the corresponding ID , cond and block values. Replace the value of msec in df1 with the corresponding value for mean in df2 .
For example, the second line in df1 has correct==0 . So, in df2 find the corresponding line, where ID=='rs' , cond==1 , block==2 and use the value for the average value ( mean=545 ) to replace the value for msec ( msec=678 ). Please note that in df1 combinations of ID, block and cond can be repeated, but each combination occurs only once in df2.