Straight to the question. Say I have a following data frame:
> head(temp)
Gender Age Agegroup
2 Male 63 61+
3 Male 60 50-60
4 Male 55 50-60
5 Male 36 30-39
7 Male 39 30-39
8 Male 63 61+
Calling a summary function:
> summary(temp)
Gender Age Agegroup
Male :864692 Min. :25.00 25-29:0
Female: 0 1st Qu.:35.00 30-39:205237
Median :45.00 40-49:235622
Mean :44.48 50-60:250977
3rd Qu.:54.00 61+ : 68807
Max. :64.00
As you can see there are zero observations for the Female factor and 25-29 factor levels. As a result, I dont need those levels. I remove them using the following code:
temp$Gender<-factor(temp$Gender)
temp$Agegroup<-factor(temp$Agegroup)
My question is: how would I use the one of the apply function to execute the code I used to remove levels? Something that will look like:
# Pseudo code just to illustrate my purpose
temp[,c(1,3)]<-apply(temp[,c(1,3)],FUN=factor)
It will be handy in case I need to update the levels of lots of columns of a data frame. Thanks. Let me know if you need more clarification.