Having issues with grouping in R

I am trying to group things such as age by age range to make histograms out of it. For example I have a data set of ages that I need to group by ages 18 to 49, 50 to 64, and greater than 65. I have watched numerous videos and read many articles. Help on how to do this would be much appreciated. Thanks!

library(tidyverse)

(example_df <- tibble(age = seq(from = 13, 
                                by = 4, length.out = 16)))

(df_with_bin <- mutate(example_df,
  mybin = cut(age,
    breaks = c(-Inf, 18, 49, 64, Inf)
  )
))

ggplot(data = df_with_bin) +
  aes(x = mybin) +
  geom_bar()

This topic was automatically closed 42 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.