dplyr group_by出错 [英] Error with dplyr group_by

查看:1820
本文介绍了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]

这篇关于dplyr group_by出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆