乘以r中的数据帧

时间:2014-09-25 14:56:25

标签: r

我有两个数据帧。一个叫做数据,比如

data <- data.frame(ID = c(1, 1, 2, 2 ),
           Number = c(1,2, 1, 2),
           Answer = c(1, 2, 3, 2 )
           )

另一个叫做权重,比如

weights <- data.frame ( Number=c(1,2), 
            weight1=c(0.5,1), 
            weight2=c(1, 1)
          )

我想要的是使用Data $ Answers根据数量(在两个数据帧中)乘以权重$ weight。最终结果应该看起来像

  ID Number Answer Answer*Weights1  Answer*Weights2
1  1      1      1   1*0.5             1*1
2  1      2      2   2*1               2*1
3  2      1      3   3*0.5             3*1
4  2      2      2   2*1               2*1

我怎样才能实现它?您的意见将深受赞赏。感谢。

4 个答案:

答案 0 :(得分:3)

data <- merge(data, weights, by = "Number")
data <- transform(data,
                  A1 = Answer * weight1,
                  A2 = Answer * weight2)
#  Number ID Answer weight1 weight2  A1 A2
#1      1  1      1     0.5       1 0.5  1
#2      1  2      3     0.5       1 1.5  3
#3      2  1      2     1.0       1 2.0  2
#4      2  2      2     1.0       1 2.0  2

答案 1 :(得分:1)

您也可以

 library(dplyr)
 left_join(data, weights, by="Number") %>% 
 select(ID:Answer, Answer_weight1=weight1, Answer_weight2=weight2) %>%
 mutate_each(funs(Answer*.), contains("weight"))
#    ID Number Answer Answer_weight1 Answer_weight2
# 1  1      1      1            0.5              1
# 2  1      2      2            2.0              2
# 3  2      1      3            1.5              3
# 4  2      2      2            2.0              2

答案 2 :(得分:1)

以下是使用data.table

执行此操作的方法
require(data.table) ## 1.9.2
setDT(data)         ## convert data.frame to data.table by reference
setDT(weights)

setkey(data, Number)    ## set the key columns to join by
data[weights, c("Answer1", "Answer2") := 
           list(Answer * weight1, Answer * weight2)]

我们执行连接,但直接创建所需的列而没有中间变量(weight1weight2),因此非常节省内存。它会修改data 到位

答案 3 :(得分:0)

如果您希望Answers*Weights1Answers*Weights2列中的这些条目成为字符串而不是实际的乘法,就像您在原始帖子中所做的那样:

data <- cbind(data,
              paste(data[, 3], weights[, 2], sep = "*"),
              paste(data[, 3], weights[, 3], sep = "*"))
names(data)[4:5] <- c("Answer*Weights1", "Answer*Weights2")
#   ID Number Answer Answer*Weights1 Answer*Weights2
# 1  1      1      1           1*0.5             1*1
# 2  1      2      2             2*1             2*1
# 3  2      1      3           3*0.5             3*1
# 4  2      2      2             2*1             2*1

或者如果你想要数字而不是字符串

data[, 4] <- data[, 3] * weights[, 2]
data[, 5] <- data[, 3] * weights[, 3]
names(data)[4:5] <- c("Answer*Weights1", "Answer*Weights2")
#   ID Number Answer Answer*Weights1 Answer*Weights2
# 1  1      1      1             0.5               1
# 2  1      2      2             2.0               2
# 3  2      1      3             1.5               3
# 4  2      2      2             2.0               2