【问题标题】:How to divide values of a data frame by the corresponding values of another data frame?如何将一个数据帧的值除以另一个数据帧的相应值?
【发布时间】:2017-05-24 19:10:03
【问题描述】:
 #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)

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

谢谢

【问题讨论】:

  • df_1[-1]/df_2[-1]怎么样

标签: r dataframe plyr


【解决方案1】:

您可以在删除第一列并将cbind 与第一列之后进行数学划分。

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

【讨论】:

  • 谢谢@TheoB。假设我有多个类似于 df_3 的数据帧,并且我想在 ggplot 中对这些值进行分布,知道如何重新格式化它们吗?
  • 我想,我需要以某种方式将最终文件(例如 df_3)转换为单列数据框。因为我不关心值的身份,只关心分布。
猜你喜欢
  • 1970-01-01
  • 2019-07-15
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 1970-01-01
相关资源
最近更新 更多