Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with dplyr group_by

Tags:

r

aggregate

dplyr

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

like image 348
user3050590 Avatar asked Jun 17 '15 15:06

user3050590


People also ask

What is group_by in dplyr?

The group_by() function in R is from dplyr package that is used to group rows by column values in the DataFrame, It is similar to GROUP BY clause in SQL. R dplyr groupby is used to collect identical data into groups on DataFrame and perform aggregate functions on the grouped data.

What's the point of using group_by ()?

Most data operations are done on groups defined by variables. group_by() takes an existing tbl and converts it into a grouped tbl where operations are performed "by group". ungroup() removes grouping.

Can you group by multiple columns in dplyr?

By using group_by() function from dplyr package we can perform group by on multiple columns or variables (two or more columns) and summarise on multiple columns for aggregations.


1 Answers

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]
like image 199
Ferroao Avatar answered Oct 15 '22 12:10

Ferroao