使用dplyr/group_by查找行数 [英] Find number of rows using dplyr/group_by
问题描述
我正在使用mtcars
数据集.我想查找特定数据组合的记录数.与SQL中的count(*)
group by子句非常相似. plyr 中的ddply()
为我工作
I am using the mtcars
dataset. I want to find the number of records for a particular combination of data. Something very similar to the count(*)
group by clause in SQL. ddply()
from plyr is working for me
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()
为我工作?
What am I doing wrong? How can I get group_by()
/ summarise()
to work for me?
推荐答案
dplyr中有一个特殊功能n()
来对行进行计数(可能在组内):
There's a special function n()
in dplyr to count rows (potentially within groups):
library(dplyr)
mtcars %>%
group_by(cyl, gear) %>%
summarise(n = n())
#Source: local data frame [8 x 3]
#Groups: cyl [?]
#
# cyl gear n
# (dbl) (dbl) (int)
#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
但是dplyr还提供了一个方便的count
函数,该函数使用更少的键入功能完全相同:
But dplyr also offers a handy count
function which does exactly the same with less typing:
count(mtcars, cyl, gear) # or mtcars %>% count(cyl, gear)
#Source: local data frame [8 x 3]
#Groups: cyl [?]
#
# cyl gear n
# (dbl) (dbl) (int)
#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
这篇关于使用dplyr/group_by查找行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!