Search code examples
rdecision-treerpart

How to make an arbitrary large number of variables be treated as factors


I have a large amount of variables that I must treat as categorical although they are represented numerically. For one variable I know I can use

train$var1 = as.factor(train$var1)

But how can I apply the same for as many variables as I want?


Solution

  • If you want to apply it to all columns (variables), you can do

    train[] <- lapply(train, as.factor)
    

    or for a subset of columns (for example 3 to 10) use

    train[3:10] <- lapply(train[3:10], as.factor)