mardi 16 novembre 2021

Recode age variable in my dataset using ifelse() function

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")

Aucun commentaire:

Enregistrer un commentaire