Tuesday, March 15, 2016

Convert data.frame columns from factors to characters

Just following on Matt and Dirk. If you want to recreate your existing data frame without changing the global option, you can recreate it with an apply statement:
bob <- data.frame(lapply(bob, as.character), stringsAsFactors=FALSE)


To replace only factors:
i <- sapply(bob, is.factor)
bob[i] <- lapply(bob[i], as.character)

No comments:

Post a Comment