问题描述
c1 c2 count
1 1 1 20
2 2 3 50
3 1 4 30
因此,我基本上具有与上表相同的数据,但是我想要做的是添加一个新列,该列的每一行计数除以具有相同c1值的所有行的计数之和,最后得到类似以下内容: / p>
c1 c2 count new_col
1 1 1 20 20/50
2 2 3 40 40/40
3 1 4 30 30/50
如您所见,新列的行计数除以所有具有c1值匹配的计数之和。 所以我一直在尝试这样的事情:
df$new_col <- df$count/sum(df[SUBSET OF ROWS THAT HAVE SAME c1 VALUE]$count)
我看过类似df [df $ c1 ==一些值,]的东西,但这仅适用于硬编码值。我也可以尝试使用for循环,但是要遍历数据中的每一行都花费太长时间,并且永远无法完成运行,我也不知道c1需要的所有值。我对R很陌生,可能对此有一个非常简单的解决方案,但我一直无法提出。
解决方法
尝试一下:
next
代码:
this._dataService.currentUserId.subscribe(userId => {
if (userId.length === 0)
{
this.userService.getUserProfile().subscribe(
res => {
this.userDetails = res['user'];
this.userId = this.userDetails._id;
this.getProducts();
},err => {
console.log(err);
}
);
} else
{
this.getProducts();
}
});
输出:
library(dplyr)
#Data
df <- structure(list(c1 = c(1L,2L,1L),c2 = c(1L,3L,4L),count = c(20,40,30)),row.names = c("1","2","3"),class = "data.frame")
,
通过ave
> within(df,new_col <- count/ave(count,c1,FUN = sum))
c1 c2 count new_col
1 1 1 20 0.4
2 2 3 40 1.0
3 1 4 30 0.6
或通过rowsum
(来自评论的@akrun)
> within(df,new_col <- count/rowsum(count,c1)[c1])
c1 c2 count new_col
1 1 1 20 0.4
2 2 3 40 1.0
3 1 4 30 0.6
数据
> dput(df)
structure(list(c1 = c(1L,class = "data.frame")