You can use the shortcut for the formula in lm()
m <- matrix(rnorm(100), ncol=5) lm(m[,1] ~ m[,2:5]
here he will be the same as
lm(m[,1] ~ m[,2] + m[,3] + m[,4] + m[,5]
but in the case when the variables do not have the same level (at least this is my assumption so far), this does not work, and I get an error:
Error in model.frame.default(formula = hm[, 1] ~ hm[, 2:4], drop.unused.levels = TRUE) : invalid type (list) for variable 'hm[, 2:4]'
Data (hm):
N cor.distance switches time 1 50 0.04707842 2 0.003 2 100 -0.10769441 2 0.004 3 200 -0.01278359 2 0.004 4 300 0.04229509 5 0.008 5 500 -0.04490092 6 0.010 6 1000 0.01939561 4 0.007
Is there any other shortcut to avoid having to write a long formula?
source share