I've got a data set with a column age_group where there are variables 0-4 years, 5-10 years etc up to 75 plus. I want to create a new column with just three variables under 16, 16-65 years and 65 plus. I've tried using if_else and grepl but I can't figure out how to write it with so many variables. I got it to create a column with true or false for is_child but I wanted to put new variables in, not just true/false. Any help really appreciated!
Asked
Active
Viewed 35 times
0
-
Please add data using `dput` and show the expected output for the same. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). Also check `?dplyr::case_when` – Ronak Shah Aug 03 '20 at 07:51
-
See `?cut`..... – Sotos Aug 03 '20 at 07:55
1 Answers
0
Since you didn't provide any data, here is a possible solution with ifelse
and some abbreviations:
ifelse(data$age_group %in% c("0-4 years", "5-10 years", etc), "under 16",
ifelse(data$age_group %in% c("16-20 years", "20-25 years", etc), "16-65 years", "65 plus"))

Nico
- 463
- 5
- 11