I am trying to recode the column age in my dataset into three rougly equal sized groups of "young", "middle", and "old", using the ifelse() function. I'm probably doing something wrong or missing something.
What I really want to do is to give the name "young" to every age group less than 28, after that I want to give the name "middle" to every age group between 28 and 53, and everything above 53 should be called "old". This is the code that I tried using, but it just converts the age column into only one name, in this case "middle".
PU6_exercise_data$age <- ifelse(PU6_exercise_data$age > 28, "middle", "young")
PU6_exercise_data$age <- ifelse(PU6_exercise_data$age > 53, "old", "middle")
CodePudding user response:
Seems like case_when() is better here. You'll have to decide where the = operator goes i.e. are 28 year olds 'young' or 'middle'?
age <- data.frame(age = c(15, 29, 54, 53, 28))
age %>%
mutate(age_bracket = case_when(age >= 28 & age < 53 ~ "middle",
age < 28 ~ "young",
age >= 53 ~ "old"))
CodePudding user response:
The problem is that in the first line you change your age column to contain only characters of either "middle" or "young". Meaning when you try to check age in the second line, you're effectively trying to check if "middle" or "young" > 53, which won't work.
You can either as jpenzer suggest use case_when()
from the dplyr package or create a second column with your age group, rather than trying to update the age column itself.
df <- data.frame(age = seq(1, 100, 25))
df$age_group <- ifelse(df$age < 28, "young", "middle")
df$age_group <- ifelse(df$age > 53, "old", df$age_group)
df
age age_group
1 1 young
2 26 young
3 51 middle
4 76 old