问题
When building the linear regression model using lm
, the data set has about 20 independent variables. Do I need to explicitly clarify them as factor
? If I have to, how can I do that? It can be very tedious to declare one by one.
回答1:
First, check which variables R has automatically converted into factors with the commande
str(mydata)
Then if you want to convert several variable into factors easily, you can do something like this: create a "mycol" variable with the No of columns you want to turn into factor
mycol <- c(1,4,5,7:15)
mydata[, mycol] <- lapply(mydata[, mycol], as.factor) # to turn them into factor var.
mydata[, -mycol] <- lapply(mydata[, -mycol], as.factor) # to turn all the others into factor var.
来源:https://stackoverflow.com/questions/26822526/regarding-handling-many-binary-independent-variables-in-lm