-2

我正在尝试使用数据框做类似的事情


更新:2013/06/14

scores <- data.frame( Type=c("Disc","HW","Disc"), Module=c(1,1,2), Score=c(14,6,14))
weights <- data.frame ( Type=c("Disc","HW"), Weight=c(0.2,0.4))

我想应用加权因子并对分数数据框求和以生成一个包含每个模块的加权和的表。结果应如下所示:

   Module   Weighted Sum
1:      1            5.2
2:      2            2.8

有人可以帮忙吗?

4

1 回答 1

1

复制该示例:

a <- data.frame(color=c("Red","Blue","Red","Green","Red","Blue","Blue"), count=c(1,2,6,4,2,1,1),include=c(1,1,1,1,0,0,1))

  color count include
1   Red     1       1
2  Blue     2       1
3   Red     6       1
4 Green     4       1
5   Red     2       0
6  Blue     1       0
7  Blue     1       1

total = sapply(levels(a$color),function(x){sum(a$count[a$color==x & a$include==1])},USE.NAMES=F)

data.frame(color=levels(a$color),total)

  color total
1  Blue     3
2 Green     4
3   Red     7
于 2013-06-13T22:03:33.913 回答