I am facing a problem in nested ifelse() statement in R. I have a dataframe which has a column Age. I have to encode the data by following condition-
- If Age <=18, then Age=child
- If Age >18 and <=60, then Age=adult
- If Age >60, then Age=senior.
I used following code to solve the problem
ifelse((Titanic$Age <= 18),Titanic$Age <-'child',ifelse((Titanic$Age>18 & Titanic$Age<=60),Titanic$Age <- 'adult',Titanic$Age <- 'senior'))
The problem I am facing is that it turns all rows in age column 'senior' even through most values range around 20-40