计算每组的行数并将结果添加到原始数据框中 [英] Count number of rows per group and add result to original data frame
问题描述
假设我有一个 data.frame
对象:
Say I have a data.frame
object:
df <- data.frame(name=c('black','black','black','red','red'),
type=c('chair','chair','sofa','sofa','plate'),
num=c(4,5,12,4,3))
现在我想计算 name
和 type
的每个组合的行数(观察值).可以这样做:
Now I want to count the number of rows (observations) of for each combination of name
and type
. This can be done like so:
table(df[ , c("name","type")])
或者也可能使用 plyr
,(虽然我不确定如何).
or possibly also with plyr
, (though I am not sure how).
但是,如何将结果合并到原始数据框中?使结果看起来像这样:
However, how do I get the results incorporated into the original data frame? So that the results will look like this:
df
# name type num count
# 1 black chair 4 2
# 2 black chair 5 2
# 3 black sofa 12 1
# 4 red sofa 4 1
# 5 red plate 3 1
count
现在存储聚合结果的位置.
where count
now stores the results from the aggregation.
使用 plyr
的解决方案也可能很有趣,但我想看看这是如何使用基础 R 完成的.
A solution with plyr
could be interesting to learn as well, though I would like to see how this is done with base R.
推荐答案
使用data.table
:
library(data.table)
dt = as.data.table(df)
# or coerce to data.table by reference:
# setDT(df)
dt[ , count := .N, by = .(name, type)]
对于 data.table 1.8.2
之前的替代方案,请参阅编辑历史记录.
For pre-data.table 1.8.2
alternative, see edit history.
使用dplyr
:
library(dplyr)
df %>%
group_by(name, type) %>%
mutate(count = n())
或者简单地说:
add_count(df, name, type)
<小时>
使用plyr
:
plyr::ddply(df, .(name, type), transform, count = length(num))
这篇关于计算每组的行数并将结果添加到原始数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!