dplyr group_by错误

use*_*590 5 aggregate r dplyr

这是我的数据集

N  Pl

10, WO
20, EI
10, WO
20, WO
30, EI
Run Code Online (Sandbox Code Playgroud)

我的预期输出是

N   Pl
10,  2
20,  1
30,  1 
Run Code Online (Sandbox Code Playgroud)

所以,基本上,我要计算每个N处的pl数

我正在尝试dplyr。我知道可能也可以使用aggregate()来完成,但是我不确定该怎么做。因此,在dplyr中,我正在运行此语句并得到以下错误

声明:

Diff %>% group_by(N) %>% summarise(pl=count(pl))
Run Code Online (Sandbox Code Playgroud)

Diff是我的表名

Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "c('integer', 'numeric')"
Run Code Online (Sandbox Code Playgroud)

我不确定该怎么做。任何帮助将不胜感激。我也只有R的基础知识

Fer*_*oao 4

也许您想要的输出是错误的,请尝试:

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]
Run Code Online (Sandbox Code Playgroud)