0

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?

talat
  • 68,970
  • 21
  • 126
  • 157
LetsPlayYahtzee
  • 7,161
  • 12
  • 41
  • 65

1 Answers1

3

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)
talat
  • 68,970
  • 21
  • 126
  • 157