regarding handling many binary independent variabl

2019-08-09 12:44发布

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条回答
Lonely孤独者°
2楼-- · 2019-08-09 13:32

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.
查看更多
登录 后发表回答