I know how to convert one factor of a dataframe to numeric:
rds$fcv12afa3num <- as.numeric(levels(rds$fcv12afa3))[rds$fcv12afa3]
My two questions:
I have many NA's, if that matters.
Thanks for your answer, Christian
Without example data, I can't give a completely exact answer, but this should get you started.
factorVars <- names(YourData)[vapply(YourData, is.factor, logical(1))]
YourData[, factorVars] <- lapply(YourData[, factorVars, drop = FALSE],
as.numeric)
Some notes:
drop = FALSE
to handle the case of there only being one factor in your data frame. as.data.frame
to get your data frame back.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.