Search code examples
rdataframedplyrsapplydata-wrangling

multiply column by row value of another dataframe in R


I have the following data:

set.seed(100)
policy.1 <- data.frame(id= 1:2450,
                      temperature.1= runif(2450, min=40, max=80),
                      temperature.2= runif(2450, min=10, max=20),
                      temperature.3= runif(2450, min=50, max=70),
                      temperature.4= runif(2450, min=10, max=20),
                      temperature.5= runif(2450, min=10, max=20),
                      temperature.6= runif(2450, min=10, max=20),
                      temperature.7= runif(2450, min=10, max=20),
                      temperature.8= runif(2450, min=10, max=20),
                      temperature.9= runif(2450, min=10, max=20),
                      temperature.10= runif(2450, min=10, max=20),
                      temperature.11= runif(2450, min=10, max=20),
                      temperature.12= runif(2450, min=10, max=20),
                      temperature.13= runif(2450, min=10, max=20),
                      temperature.14= runif(2450, min=10, max=20),
                      temperature.15= runif(2450, min=10, max=20),
                      temperature.16= runif(2450, min=10, max=20),
                      temperature.17= runif(2450, min=10, max=20),
                      temperature.18= runif(2450, min=10, max=20),
                      temperature.19= runif(2450, min=10, max=20),
                      temperature.20= runif(2450, min=10, max=20),
                      temperature.21= runif(2450, min=10, max=20))
                      

policy.2 <- data.frame(year= (2000:2020),
time=c(44,55,66,77,88,89,90,91,94,100,105,106,107,111,115,117,120,124,127,130,134))

I want to do the following operation on each cell of policy.1 I want to multiply the first row of column of policy.1$temperature.1 by 100 and then divide it by the first row of column policy.2$time. I want to repeat the same operation with the next row of policy.1$temperature.1 with the first row of column policy.2$time. After I complete policy.1$temperature.1, I want to move on and to policy.1$temperature.2 but this time divide by the second row of policy.2$time

I would also like the output as a dataframe.

Thank you


Solution

  • Here's a simple example of two methods that should both scale up well:

    ## small example
    set.seed(100)
    n = 5
    policy.1 <- data.frame(id= 1:5,
                          temperature.1= runif(5, min=40, max=80),
                          temperature.2= runif(5, min=10, max=20),
                          temperature.3= runif(5, min=50, max=70))
    policy.2 <- data.frame(year= (2001:2003), time=c(44,55,66))
    
    ## solution 1 - for loop
    result = policy.1
    for(i in 1:(ncol(result) - 1)) {
      result[[i + 1]] = result[[i + 1]] * 100 / policy.2$time[i]
    }
    result
    #   id temperature.1 temperature.2 temperature.3
    # 1  1     118.88783      26.97765      94.69686
    # 2  2     114.33386      32.95277     102.48986
    # 3  3     141.12022      24.91492      84.25315
    # 4  4      96.03483      28.11925      87.83297
    # 5  5     133.50448      21.27749      98.86518
    
    ## solution 2 - direct replacement
    result2 = policy.1
    result2[-1] = t(t(result2[-1]) * 100 / policy.2$time)
    result2
    #   id temperature.1 temperature.2 temperature.3
    # 1  1     118.88783      26.97765      94.69686
    # 2  2     114.33386      32.95277     102.48986
    # 3  3     141.12022      24.91492      84.25315
    # 4  4      96.03483      28.11925      87.83297
    # 5  5     133.50448      21.27749      98.86518