r - 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 ()
解决方案
推荐阅读
- java - 将 Java 代码添加到 Cordova Android 应用程序的正确方法是什么?
- javascript - Sequelize 在同一张桌子上的关系。基于多对多检索数据
- r - 在闪亮的 R 中使用 plotly 的多线图
- javascript - 无法呈现解构的标题道具
- google-apps-script - 从谷歌表格发送个性化电子邮件
- javascript - JSON(仍然)是有效的 JavaScript 代码吗?将对象从后端移动到前端
- json - 当我从数据文件中获取数据时,我的 json 请求的 Oder 按字母顺序排序。我不希望我的 json 请求得到排序
- flutter - 底部溢出 418 像素的 RenderFlex
- apache-kafka - Kafka分配分区,是否需要commit offsets
- javascript - 无按钮显示元素计数