如何按组将唯一值的计数添加到R数据中。我希望通过分组第二个变量来计数唯一值的数量,然后将计数作为一个新列添加到现有的data.framework中。例如,如果现有的数据框架如下所示: color type1 black chair2 black chair3 black sofa4 green sofa5 green sofa6 red sofa7 red plate8 blue sofa9 blue plate10 blue chair我想为每个color,唯一的数types现有数据: color type unique_types1 black chair 22 black chair 23 black sofa 24 green sofa 15 green sofa 16 red sofa 27 red plate 28 blue sofa 39 blue plate 310 blue chair 3我希望用ave,但似乎找不到一个直接的方法,不需要很多行。我有>100,000行,所以我也不确定效率有多重要。它有点类似于这个问题:每组计数观察/行数,并将结果添加到数据帧中
3 回答
慕尼黑5688855
TA贡献1848条经验 获得超2个赞
ave
within(df, { count <- ave(type, color, FUN=function(x) length(unique(x)))})
type
data.table
require(data.table)setDT(df)[, count := uniqueN(type), by = color] # v1.9.6+# if you don't want df to be modified by referenceans = as.data.table(df)[, count := uniqueN(type), by = color]
uniqueN
v1.9.6
length(unique(.))
require(plyr)ddply(df, .(color), mutate, count = length(unique(type)))
aggregate
:
agg <- aggregate(data=df, type ~ color, function(x) length(unique(x)))merge(df, agg, by="color", all=TRUE)
holdtom
TA贡献1805条经验 获得超10个赞
unique
table
tabulate
df$color
factor
table(unique(df)$color)[as.character(df$color)]# black black black green green red red blue blue blue # 2 2 2 1 1 2 2 3 3 3
tabulate(unique(df)$color)[as.integer(df$color)]# [1] 2 2 2 1 1 2 2 3 3 3
df$color
character
table(unique(df)$color)[df$color]
df$color
integer
tabulate(unique(df)$color)[df$color]
- 3 回答
- 0 关注
- 768 浏览
添加回答
举报
0/150
提交
取消