9
votes

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!

4
Shouldn't the code for count be count(dataset, Nationality)? - aosmith
It works! Thanks. Strangely before it accepted the dataset$Nationality format. - thixio
@thixio Upvote and accept my answer if you think it is useful so that others know this is the right answer, thanks! - Ankit Gupta

4 Answers

18
votes

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)

6
votes

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)
5
votes

Writing what I feel is a more complete answer that combines what Ankit and aosmith have said and adds to it.

count() in the dplyr package can be called from two different contexts. The first is what aosmith and Ankit have suggested, which calls count() with the data frame as the first argument, followed by the variable(s) you want to count, separated by commas.

count(df, var1)

or

count(df, var1, var2)

The second way you can do this is using the R pipe (%>%) to pass the data frame into count().

df %>% count(var1)

or

df %>% count(var1, var2)

Failing to provide the data frame in either context will result in the 'no applicable method' error seen in the question.

0
votes

If you mis-use count() inside of a group_by(...) %>% summarise(...), you will get this error

For example, this code will produce the error:

ufo_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 case, I wanted the n() "summary function" instead (more info in the summarise documentation)

ufo_tibble %>% 
  group_by(state) %>% 
  summarize(number_sightings = n())