我正在使用mtcars
数据集。 我想找到特定数据组合的记录数。 类似于SQL中count(*)
group by子句的内容。 plyr中的ddply()
对我有用。
library(plyr)
ddply(mtcars, .(cyl,gear),nrow)
有输出
cyl gear V1
1 4 3 1
2 4 4 8
3 4 5 2
4 6 3 2
5 6 4 4
6 6 5 1
7 8 3 12
8 8 5 2
使用此代码
library(dplyr)
g <- group_by(mtcars, cyl, gear)
summarise(g, length(gear))
有输出
length(cyl)
1 32
我发现各种可以用作传递给summarise()
的函数,但没有一个对我有效。我找到了一个函数sum(G)
,它返回
I found various functions to pass in to summarise()
but none seem to work for me. One function I found is sum(G)
, which returned
Error in eval(expr, envir, enclos) : object 'G' not found
尝试使用n()
,返回了
Error in n() : This function should not be called directly
我做错了什么?我该如何让group_by()
/ summarise()
对我起作用?
g
实际上没有被分组时才有意义。因此,我会寻找你可能以某种方式取消分组数据框的原因。 - joran