Error with dplyr group_by

泪湿孤枕 提交于 2020-01-03 13:29:06

问题


This is my dataset

N  Pl

10, WO
20, EI
10, WO
20, WO
30, EI

My expected output is

N   Pl
10,  2
20,  1
30,  1 

So, basically, I am counting number of pl with each value at N

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

Statement:

Diff %>% group_by(N) %>% summarise(pl=count(pl))

Here Diff is my table name

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

I am not sure how to do that. Any help will be appreciated. Also I have only basic knowledge of R


回答1:


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]


来源:https://stackoverflow.com/questions/30895876/error-with-dplyr-group-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!