I have a dataframe with 100 variables, of which I would like a subset, say dataframename[,30:50] to be converted to their original numeric values (1,2,3,4,5).
I know that I should use as.numeric(levels(f))[f] when I convert a factor, but I can only make this work when I convert the factors one at a time. I would like to convert them all at once.
This wont work:
as.numeric(levels(dataframename[,30:50]))[dataframename[,30:50]]
neither will this:
sapply(dataframename[,30:50],as.numeric(levels(dataframename[,30:50]))
[dataframename[,30:50]]
Any ideas or stuff I should read?
This is a smaller example but the idea should hold. You can use
lapplyto apply your conversion to each column of your data frame and then just replace those columns directly.