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.