I am having problems converting a list to a factor in R, for data analysis. I want to bundle my age variable and am getting a list. When I try to convert it to a factor I get the following error :
Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?
This is my code:
group_age <- function(age) {
if (age>=0&age<=20) {
return("Age of 0-20")
} else if(age>20&age<=30) {
return("Age of 21-30")
} else if (age>30&age<=40) {
return("Age of 31-40")
} else if (age>40&age<=50) {
return("Age of 41-50")
} else if (age>60) {
return("More than 60")
}
}
edu$age_group <- sapply(edu$age,group_age)
edu$age_group <- as.factor(edu$age_group)
Any solution?