[R] data frame as X in linear model lm() ?

ivo welch ivowel at gmail.com
Sat Mar 25 15:59:53 CET 2006


Dear R wizards:  This must have an obvious solution, but I am stumped.
 I can run a linear regression giving a matrix as the independent set
of variables, but if I give a data frame (which I would like to give,
because it should tell the linear model the names of the variables), R
does not like it.  An example is:

N=20; y= rnorm(N);
x.m <- (matrix( nrow=N, ncol=2 ));
x.m[,1]=rnorm(N); x.m[,2]=rnorm(N);

print(summary(lm(y ~ x.m)));  # works

x.d <- as.data.frame(x.m);
names(x.d) <- c("one.rv", "two.rv");  # I would like the summary to
know my variable names!

print(summary(lm(y ~ x.d)));  # fails

[please indulge my semi-colons.]  how should this be done?  help, as
always, appreciated.

sincerely,

/ivo welch




More information about the R-help mailing list