[R] using subset() in data frame
Robert Walters
pungo9nc at embarqmail.com
Sat Feb 23 02:01:34 CET 2008
R folks,
As an R novice, I struggle with the mystery of subsetting. Textbook and
online examples of this seem quite straightforward yet I cannot get my
mind around it. For practice, I'm using the code in MASS Ch. 6,
"whiteside data" to analyze a different data set with similar variables
and structure.
Here is my data frame:
###subset one of three cases for the variable 'position'
>data.b<-data.a[data.a$position=="inrow",]
> print(data.b)
position porosity x y
1 inrow macro 1.40 16.5
2 inrow macro . .
. . . .
. . . .
7 inrow micro
8 inrow micro
Now I want to do separate lm's for each case of porosity, macro and
micro. The code as given in MASS, p.141, slightly modified would be:
fit1 <- lm(y ~ x, data=data.b, subset = porosity == "macro")
fit2 <- update(fit1, subset = porosity == "micro")
###simplest code with subscripting
fit1 <- lm(y ~ x, data.b[porosity=="macro"])
###following example in ?subset
fit1 <- lm(y ~ x, data.b, subset(data.b, porosity, select=macro))
None of th above, plus many permutations thereof, works.
Can anyone educate me?
Thanks,
Robert Walters
More information about the R-help
mailing list