如何将数据帧的值除以另一个数据帧的相应值?
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)
鉴于实际数据帧非常大,我正在寻找一种有效的方法。
谢谢
您可以在删除第一列后进行数学除法,cbind
它与第一列相除。
df_3 <- cbind(df_1[1],round(df_1[-1]/df_2[-1],1))
#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)
鉴于实际数据帧非常大,我正在寻找一种有效的方法。
谢谢
您可以在删除第一列后进行数学除法,cbind
它与第一列相除。
df_3 <- cbind(df_1[1],round(df_1[-1]/df_2[-1],1))