count() function/argument inside of summarize() function

Hey everyone, beginner to R. Quick question: why doesn't my code below work? I've noticed that just in general, whenever I use count() inside of summarize for anything, it never works, but other aggregate functions like sum, max, min, etc., tend to work. What is going on?

Code:

starwars %>%
filter(gender == "feminine") %>%
group_by(gender) %>%
summarize(how_many_women = count(gender)) %>%
View()

I do not use {dplyr } so I am not sure though using both filter and group may be overkill? I think this does what you want

starwars %>%
filter(gender == "feminine") %>%
summarize(how_many_women = n())

I see the function count() as alternative to group_by/summarise
As the man page says:

df %>% count(a, b) is roughly equivalent to df %>% group_by(a, b) %>% summarise(n = n())

n() is what you want in the summarise if you want to summarise multiple genders
nrow() would be a way of just counting rows having filtered

The problem here is that count() takes a table as input, whereas gender here represents the vector contained in the column gender. From the documentation for count()`, which you can find by running

?count

or going to: Count the observations in each group — count • dplyr :

Arguments
x — A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.