1
votes

I'm trying to calculate the following proportion for each city: mean(age < 25).

My code so far is the following:

namevar <- data %>% group_by(city) %>% mean (age < 25).

My data is clean and has no NA.

If I use mean(age <25) it works, but when I use the group_by function it doesn't.

This is the message that appears:

In mean.default(unlist(x, use.names = FALSE, recursive = TRUE), : argument is not numeric or logical: returning NA

Thanks a lot for reading and helping :)

1

1 Answers

0
votes

We can use mutate (if we want to create a new column) or summarise (if needed to summarise)

library(dplyr)
data1 <-  data %>%
       group_by(city) %>%
       summarise(Prop = mean(age < 25))