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!
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)
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)
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With