Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Error in UseMethod("groups") : no applicable method for 'groups' applied to an object of class "character"

Tags:

r

count

dplyr

I was using "count" to summarize the column of a dataframe. The column (dataset$Nationality) consists of nationalities that can occur more than once (eg. Swiss, German, French, Swiss, etc.). It worked fine, until it today suddenly produced an error message:

Error in UseMethod("groups") : no applicable method for 'groups' applied to an object of class "character".

I reinstalled dplyr but it still did not work.

For example:

dataset$Nationality consists of c("Swiss", "French","German","Swiss")

then

count(dataset$Nationality) 

would give something like

Swiss,2, French, 1, German,1

Can somebody tell me what I could do to make it work again?

Thanks a lot!

like image 562
thixio Avatar asked Aug 31 '17 16:08

thixio


3 Answers

Hope this Answers your Question.

If you are using "plyr" Library library(plyr)

Correct Syntax: count(dataset$Nationality)

If you are using "dplyr" Library library(dplyr)

Correct Syntax: count(dataset, Nationality)

If you are using Both, You will have to use the syntax of the library you added last.

Adding what Dzej Suggested:

We can also use a specific library as well.

Syntax to use dplyr directly: dplyr::count(dataset, Nationality)

Syntax to use plyr directly: plyr::count(dataset$Nationality)

like image 78
Ankit Gupta Avatar answered Oct 29 '22 03:10

Ankit Gupta


According to Ankit Gupta's answer:

If you are using both, plyr and dplyr, you can use specific library's function by providing library_name::function_name(params), ex:

plyr::count(dataset$nationality)
like image 41
Dzej Bi Avatar answered Oct 29 '22 02:10

Dzej Bi


If you mis-use count() inside of a group_by(...) %>% summarise(...), you will cause the error described in this question.

For example, this code will produce the error:

tibble %>% 
  group_by(state) %>% 
  summarize(number_sightings = count(`duration (seconds)`))

count() (in dplyr::count(..., ..., [...]) and plyr::count(...)) produces a "value count"; a list of values and the frequencies of the distinct values in the vector. (vs a single number showing the length of the vector )

In this my, I wanted the n() "summary function" instead of count() (more info in the summarise documentation)

tibble %>% 
  group_by(state) %>% 
  summarize(number_sightings = n())
like image 30
The Red Pea Avatar answered Oct 29 '22 01:10

The Red Pea