我正在尝试使用数据框执行与this类似的操作。
更新: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
有人可以帮忙吗?
答案 0 :(得分: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