Jack
Jack

Reputation: 165

How to divide values of a data frame by the corresponding values of another data frame?

 #e.g. each number in df1 needs to be divided by the correponding value in df2
    Var<- c("t1","t2","t3","t4","t5","t6","t7","t8","t9","t10","t11")
    value1 <- c(2,3.1,4.5,5.1,6.5,7.1,8.5,9.11,10.1,11.8,12.3) 
    value2 <- c(2.5,3.1,4.5,5.1,12,7.1,8.5,9.11,10.1,17.8,12.3) 

    df_1 <- data.frame(Var,value1,value2)

    Var<- c("t1","t2","t3","t4","t5","t6","t7","t8","t9","t10","t11")
    value1 <- c(2.2,3.3,4.5,5.1,6.5,13,0,0,10.1,1,12.3) 
    value2 <- c(2.7,3.3,4.6,5.1,6.5,13,0,0,15.1,5.1,12.3) 

    df_2 <- data.frame(Var,value1,value2)

Given that the actual dataframes are very large, I'm looking for an efficeinct way of doing it.

Thanks

Upvotes: 0

Views: 93

Answers (1)

TheoB
TheoB

Reputation: 139

You can divide mathematically after removing the first column and cbind it with the first one.

df_3 <- cbind(df_1[1],round(df_1[-1]/df_2[-1],1))

Upvotes: 1

Related Questions