如何将数据帧的值除以另一个数据帧的相应值?

时间:2017-05-24 19:10:04

标签: r dataframe plyr

 #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)

鉴于实际的数据帧非常大,我正在寻找一种有效的方法。

由于

1 个答案:

答案 0 :(得分:1)

您可以在删除第一列后以数学方式划分,并将cbind与第一列划分。

df_3 <- cbind(df_1[1],round(df_1[-1]/df_2[-1],1))
相关问题