首页 > 解决方案 > R计数权重〜相似但不同的问题

问题描述

set.seed(1)
data = data.frame("person"=c(1:100),
                  "group"=c(sample(1:2,100,r=T)),
                  "type"=c(sample(1:3,100,r=T)),
                  "value"=c(sample(20:40,100,r=T)),
                  "weight"=c(sample(100:300,100,r=T)),
                  "score" = c(sample(1:3,100,r=T)))

我的目标是首先按“值”聚合,然后将“权重”应用于分数。所需的输出如下----

want = data.frame("value"=sort(rep(unique(data$value),6)),
                  "group"=rep(1:2,63),
                  "type"=rep(1:3,42),
                  "score"=sample(1:3,126,r=T))

但是,我的目标是让“想要的”数据框包含 WEIGHTED 分数变量。

--

另一个受@Maurits Evers 启发的例子谢谢。我想这就是我的目标。

data = data.frame("person"=c(1,2,3,4,5,6,7,8,9,10),
                  "group"=c(1,1,1,2,2,1,1,2,2,2),
                  "type"=c(1,1,2,3,3,3,3,2,2,1),
                  "value"=c(5,6,5,5,8,9,12,12,7,6),
                  "score"=c(3,3,2,3,3,2,1,1,1,1),
                  "weight"=c(100,200,100,200,150,300,100,200,200,300))
data$scoreWEIGHT = data$score * data$weight
aggregate(data$scoreWEIGHT, by=list(Category=data$value), FUN=sum)
#but i aim to use the count function ()

标签: rweightedpsych

解决方案


推荐阅读