in the next dataset, I would like to multiply the value in the Size column by the value in the Month1 , Month2 or Month3 column, depending on what number we have in the Month column. Therefore, if the value of Month is 2 in a particular row, I would like to multiply the value in the Size column by the value in the Month2 column and save the result in a new Month2 column. Thanks so much for your help in advance!
Orig = c("A","B","A","A","B","A","A","B","A") Dest = c("B","A","C","B","A","C","B","A","C") Month = c(1,1,1,2,2,2,3,3,3) Size = c(30,20,10,10,20,20,30,50,20) Month1 = c(1,0.2,0,1,0.2,0,1,0.2,0) Month2 = c(0.6,1,0,0.6,1,0,0.6,1,0) Month3 = c(0,1,0.6,0,1,0.6,0,1,0.6) df <- data.frame(Orig,Dest,Month,Size,Month1,Month2,Month3) df Orig Dest Month Size Month1 Month2 Month3 1 AB 1 30 1.0 0.6 0.0 2 BA 1 20 0.2 1.0 1.0 3 AC 1 10 0.0 0.0 0.6 4 AB 2 10 1.0 0.6 0.0 5 BA 2 20 0.2 1.0 1.0 6 AC 2 20 0.0 0.0 0.6 7 AB 3 30 1.0 0.6 0.0 8 BA 3 50 0.2 1.0 1.0 9 AC 3 20 0.0 0.0 0.6