Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change class of multiple columns in data frame without for loop [duplicate]

Tags:

dataframe

r

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?

like image 249
Scransom Avatar asked Dec 08 '22 23:12

Scransom


1 Answers

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)
like image 187
Scransom Avatar answered Mar 22 '23 23:03

Scransom