rdataframer-factor

Coerce multiple columns to factors at once


I have a sample data frame like below:

data <- data.frame(matrix(sample(1:40), 4, 10, dimnames = list(1:4, LETTERS[1:10])))

I want to know how can I select multiple columns and convert them together to factors. I usually do it in the way like data$A = as.factor(data$A). But when the data frame is very large and contains lots of columns, this way will be very time consuming. Does anyone know of a better way to do it?


Solution

  • Choose some columns to coerce to factors:

    cols <- c("A", "C", "D", "H")
    

    Use lapply() to coerce and replace the chosen columns:

    data[cols] <- lapply(data[cols], factor)  ## as.factor() could also be used
    

    Check the result:

    sapply(data, class)
    #        A         B         C         D         E         F         G 
    # "factor" "integer"  "factor"  "factor" "integer" "integer" "integer" 
    #        H         I         J 
    # "factor" "integer" "integer"