I want to change the class of multiple columns in an R data frame without either doing it one by one, or using a for
loop (and noting this answer). I can do it with either of these methods, but they feel clumsy. Note that I don't necessarily want to change every column.
E.g. I have data frame mydf
:
mydf <- data.frame("col1" = c(1, 2, 3),
"col2" = c("a", "b", "c"),
"col3" = c("a", "a", "b"), stringsAsFactors = FALSE)
I want to change columns two and three to class factor. (In reality I want to deal with many more than two columns...)
I can either do it column by column in my favourite way, e.g.:
mydf$col2 <- as.factor(mydf$col2)
mydf[, 3] <- as.factor(mydf[,3])
Or I could use a for loop:
for (i in 2:3{
mydf[,i] <- as.factor(mydf[,i])
}
These work, but feel clunky and suboptimal.
Better ideas?
OK I worked it out while writing the question, but figured it might as well go up in case it's use to anyone in future:
mydf[,2:3] <- lapply(mydf[,2:3], as.factor)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With