This question is related to this question Cleaning up factor Levels collapsing multiple Level labels but I would like to extend this to a data table and collapse the factor levels for a subset of columns of my data table. I always struggle using lapply within data table...
Here is my MWE and what I would like to achieve using levels() for two columns separately
df<-data.table(Index=1:3,factor1=c("Yes", "No", "0"), factor2=c("yes","no","no"))
str(df)
subset_factor<-c("factor1", "factor2")
label.yesno<- list("Yes" = c("Yes","yes"),
"No" = c("No", "no"))
df[,(subset_factor):=lapply(.SD,factor),.SD=subset_factor]
str(df)
levels(df$factor1)<-label.yesno
levels(df$factor2)<-label.yesno
df
I was hoping that I could use the list directly when I create the factors
df[,(subset_factor):=lapply(.SD,factor, labels=label.yesno),.SD=subset_factor]
or that I could use the Levels factor in another step somehow.. But I cannot find anything similiar. I actually would like the "0" to be transformed into NA as it is done in my MWE.