dplyr计数一个特定变量值的数量 [英] dplyr count number of one specific value of variable
问题描述
说我有一个像这样的数据集:
id <-c(1、2、2、3 ,3)
代码<-c( a, b, a, a, b, b)
dat<--data.frame(id ,代码)
即,
id代码
1 1 a
2 1 b
3 2 a
4 2 a
5 3 b
6 3 b
使用dplyr,我如何计算每个id有多少个
即,
id countA
1 1 1
2 2 2
3 3 0
我正在尝试这样的东西
countA<-dat%>%
group_by(id)%>%
summarise(cip.completed =计数(code == a))
以上给我一个错误,错误:没有将'group_by_'的适用方法应用于逻辑类的对象。
感谢您的帮助!
请尝试以下操作:
library(dplyr)
dat%>%group_by(id )%>%
summarise(cip.completed = sum(code == a)))
来源:本地数据帧[3 x 2]
id cip。已完成
(dbl)(int)
1 1 1
2 2 2
3 3 0
之所以有用,是因为逻辑条件 code == a
只是一系列零和一,而该系列的总和为发生次数。
请注意,您不必在摘要$内使用
dplyr :: count
c $ c>无论如何,因为它是总结
的包装,因此调用 n()
或 sum()
本身。请参见?dplyr :: count
。如果您真的想使用 count
,我想您可以通过首先过滤数据集以仅保留 code == a
,然后使用 count
将为您提供所有严格为正(即非零)的计数。例如,
dat%>%filter(code == a)%>%count(id)
来源:本地数据帧[2 x 2]
id n
(dbl)(int)
1 1 1
2 2 2
Say I have a dataset like this:
id <- c(1, 1, 2, 2, 3, 3)
code <- c("a", "b", "a", "a", "b", "b")
dat <- data.frame(id, code)
I.e.,
id code
1 1 a
2 1 b
3 2 a
4 2 a
5 3 b
6 3 b
Using dplyr, how would I get a count of how many a's there are for each id
i.e.,
id countA
1 1 1
2 2 2
3 3 0
I'm trying stuff like this which isn't working,
countA<- dat %>%
group_by(id) %>%
summarise(cip.completed= count(code == "a"))
The above gives me an error, "Error: no applicable method for 'group_by_' applied to an object of class "logical""
Thanks for your help!
Try the following instead:
library(dplyr)
dat %>% group_by(id) %>%
summarise(cip.completed= sum(code == "a"))
Source: local data frame [3 x 2]
id cip.completed
(dbl) (int)
1 1 1
2 2 2
3 3 0
This works because the logical condition code == a
is just a series of zeros and ones, and the sum of this series is the number of occurences.
Note that you would not necessarily use dplyr::count
inside summarise
anyway, as it is a wrapper for summarise
calling either n()
or sum()
itself. See ?dplyr::count
. If you really want to use count
, I guess you could do that by first filtering the dataset to only retain all rows in which code==a
, and using count
would then give you all strictly positive (i.e. non-zero) counts. For instance,
dat %>% filter(code==a) %>% count(id)
Source: local data frame [2 x 2]
id n
(dbl) (int)
1 1 1
2 2 2
这篇关于dplyr计数一个特定变量值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!