[R] using subset() in data frame

From: Robert Walters <pungo9nc_at_embarqmail.com>
Date: Fri, 22 Feb 2008 20:01:34 -0500

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'

> 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?


Robert Walters

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 23 Feb 2008 - 01:05:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 23 Feb 2008 - 11:30:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive