0

Hello everyone, I am very new to R and I am trying to solve a problem that I encounter. I have a column for Age and I am trying to create another column that put different age into differebt group. The below code is my attempt to do it but it wont work.


demographic_data %>% 
IF(age %in% 25:29) [gen <- 'Gen-Y1'] ELSE IF (age %in% 30:39)[gen <-'Gen-Y2'] 
ELSE IF(age %in% 40:56)[gen <-'Gen-X'] ELSE [gen <-'Boomers']

I appreciate your time to explain to me whorever you are.

Billy C
  • 25
  • 3
  • This is a classic `cut` problem too - `cut(demographic_data$age, c(0,25,30,40,56,Inf), labels=c("Gen-Z","Gen-Y1","Gen-Y2","Gen-X","Boomers"))` - see various questions like: https://stackoverflow.com/questions/2647639/create-categorical-variable-in-r-based-on-range – thelatemail Sep 13 '21 at 04:46
  • 1
    Wow, i never learnt the cut command and it is super useful! thank you so much! – Billy C Sep 13 '21 at 22:42

1 Answers1

0

Here is a solution:

library(dplyr)

demographic_data %>% 
  mutate(gen = case_when(
    between(age,25,29) ~ 'Gen-Y1',
    between(age,30,39) ~ 'Gen-Y2',
    between(age,40,56) ~ 'Gen-X',
    TRUE ~ "Boomers"
  ))

But there is a problem, since an age < 25 would be classified as "Boomers"

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32