有没有办法以更有效的方式(迭代或通用脚本)更改每列中的因子级别。列将被修改为具有级别1到r,其中是该因子中的级别数。
目前,我正在通过为每列编写命令来修改它们:
setattr(lizards$Diameter,"levels",c(1,2))
setattr(lizards$Species,"levels",c(1,2))
setattr(lizards$Height,"levels",c(1,2))
str(lizards)
lizards 409 obs. of 3 variables
Species : Factor w/ 2 levels "Sagrei","Distichus": 1 1...
Diameter: Factor w/ 2 levels "narrow","wide": 1 1 1 1 ...
Height : Factor w/ 2 levels "high","low": 2 2 2 2 2 2 ...