在R

时间:2015-11-08 18:39:15

标签: r dataframe division

如何将一个数据帧除以另一个数据帧?两个数据帧具有相同的列和相同的行,但是我需要将每个交叉点与其相应的交叉点分成新的数据帧,例如,下面:

DF1
Name    Jan    Feb    Mar
Aaron     2      4      3
Blake     5      6      4

DF2
Name    Jan    Feb    Mar
Aaron     4      6      6
Blake     7      6      5

DF1/DF2 = DF3

DF3 (result)
Name    Jan    Feb    Mar
Aaron   0.5    0.7    0.5
Blake   0.7    1.0    0.8

我正在使用子集然后dcast来构建每个数据框,但很难弄清楚如何划分它们。谢谢你的帮助!

2 个答案:

答案 0 :(得分:6)

我们将'DF1'和'DF2'中的数字列(通过删除第一列)和cbind分隔为第一列。

DF3 <- cbind(DF1[1],round(DF1[-1]/DF2[-1],1))
DF3
#    Name Jan Feb Mar
# 1 Aaron 0.5 0.7 0.5
# 2 Blake 0.7 1.0 0.8

答案 1 :(得分:2)

由于您提到您使用subsetdcast来构建每个数据框,我怀疑您已将这些数据全部放在一个数据框中,在这种情况下分配numerator的角色您可能需要执行denominator才能使用ddply运行计算。例如,使用您的示例数据并将其熔化回长格式数据框,将为您提供以下单ddply

# data
DF1 <- data.frame(Name = c("Aaron", "Blake"), Jan = c(2, 5), Feb = c(4, 6), Mar = c(3, 4))
DF2 <- data.frame(Name = c("Aaron", "Blake"), Jan = c(4, 7), Feb = c(6, 6), Mar = c(6, 5))

# long format with 'numerator' and 'denominator' roles assigned
# (unnecessary if you already have long format, just assign numerator/denomninator)
library(reshape2)
df <- rbind(
  transform(
    melt(DF1, id.vars = "Name", variable.name = "Month"),
    role = "numerator"),
  transform(
    melt(DF2, id.vars = "Name", variable.name = "Month"),
    role = "denominator")
)

# ddply 
library(plyr)
ddply(df, .(Name, Month), summarize, 
      Result = value[role == "numerator"] / value[role == "denominator"])

#   Name Month     Result
# 1 Aaron   Jan 0.5000000
# 2 Aaron   Feb 0.6666667
# 3 Aaron   Mar 0.5000000
# 4 Blake   Jan 0.7142857
# 5 Blake   Feb 1.0000000
# 6 Blake   Mar 0.8000000