Re: [S] Another pestering newbie question

Marc R. Feldesman (
Fri, 20 Feb 1998 14:02:46 -0800

Thanks to all who replied to my question about how to subset a dataframe to
create a model of the form:

group~x1+x2+x3+x4...+x8, where group and x1 - x8 are part of a larger
dataframe. My question concerned how to reference the variables without
having to type their full names. I received many excellent (and nearly
identical replies), but Dave Krantz's reply is nicely pedagogical for the
benefit of all. I hope Dave doesn't mind me forwarding his message, in
particular, to the whole group. Thanks also to Bert Gunter, Shawn Boles &
Rolf Turner for their replies [which say pretty much the same thing as
Dave's reply].

>Suppose that `group' is column 1 of the dataframe x,
>and x1,...,x8 are columns 11-18 of x (for example).
>Then the following will work:
> lm(group ~ . , data=x[ , c(1, 11:18)])
>This makes use of three ideas: (1) the syntactic device
>`group ~ .', which constructs a linear model for group with
>ALL variables in the dataframe except `group' as predictors;
>(2) the fact that the indexing operation x[ , c(1, 11:18)]
>produces a new dataframe with exactly the variables you need;
>and (3) the fact that specifying `data=y' forces the dataframe
>y to the top of the search hierarchy, within the call to lm().
>The same device should work equally well for modelling functions
>other than lm().
>In my own practice, I rarely manage to keep just the variables that I
>want to use for modelling, such as x1, ...,x8 together in consecutive
>columns of a dataframe; so I would not have a neat numeric syntax
>such as `11:18' available. An alternative is to keep on hand a
>separate vector `xnames' with the names of the desired explanatory
>variables; then you can use the syntax
> lm(group ~ . , data=x[ , c("group", xnames)])
>to accomplish your purpose. This has the advantage that you can
>just change xnames to produce a different model.
>Dave Krantz (

Dr. Marc R. Feldesman
pager: 503-870-2515
fax: 503-725-3905

"If ignorance is bliss then why aren't there more happy people?" Lawrence

Powered by: Monstrochoerus - the 300 MHz Pentium II
This message was distributed by To unsubscribe
send e-mail to with the BODY of the
message: unsubscribe s-news