dplyr group_by出错
问题描述:
这是我的数据集
N Pl
10, WO
20, EI
10, WO
20, WO
30, EI
我的预期输出是
N Pl
10, 2
20, 1
30, 1
所以,基本上,我正在计数每个值为N的pl的数字
So, basically, I am counting number of pl with each value at N
我正在尝试dplyr。我知道大概这也可以用aggregate()完成,但我不知道该怎么做。所以在dplyr我正在运行这个声明并得到以下错误
I am trying dplyr. I know probably this can also be done with aggregate() but I am not sure how to do with that. So in dplyr I am running this statement and getting the following error
声明:
Diff %>% group_by(N) %>% summarise(pl=count(pl))
这里 Diff
是我的表名
Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "c('integer', 'numeric')"
我不知道该怎么做任何帮助将不胜感激。另外我只有R的基本知识
I am not sure how to do that. Any help will be appreciated. Also I have only basic knowledge of R
答
也许你想要的输出是错误的,尝试:
Maybe your desired output is wrong, try:
library(dplyr)
df<-data.frame(N=c(10,20,10,20,30), Pl=c("WO","EI","WO","WO","EI"))
group <- group_by(df, N)
result <- as.data.frame(summarise(group, Pl = n_distinct(Pl)))
result
N Pl
1 10 1
2 20 2
3 30 1
# the data.table way
library(data.table)
setDT(df)[, list(Pl=uniqueN(Pl)), by= N]