[R] using subset() in data frame

Robert Walters pungo9nc at embarqmail.com
Sat Feb 23 15:13:56 CET 2008


Chuck Cleland wrote:
> On 2/23/2008 6:09 AM, Chuck Cleland wrote:
>> On 2/22/2008 8:01 PM, Robert Walters wrote:

Chuck,
Thanks for the pointers on subset(). When I submit the two variants you 
suggested, below:

 > fit1 <- lm(y ~ x, subset(data.b, porosity == "macro"))
Error in eval(expr, envir, enclos) : object "porosity" not found

 > fit1 <- lm(pore.pct ~ Db, data.b[data.b$porosity=="macro",])
Error in lm.fit(x, y, ...) :
   0 (non-NA) cases

So, the problem is there are no objects defined for data.b, as confirmed
by the following query:

 > objects()
[1] "data.a"  "data.a0" "data.b"

 > objects(data.b)
Error in as.environment(pos) : invalid object for 'as.environment'

However, when I submit the following queries:
 > names(data.b)
[1] "system"   "block" "position" ...

 > levels(data.b$porosity)
[1] "macro " "micro"

 > print(data.b$porosity)
  [1] macro...
[26] micro...
[51] macro...
[76] micro...

R returns values in each instance. Therein lies my confusion. 
Furthermore,I can plot data.b with xyplot(), a very nice coplot is 
produced.

Any thoughts?

Robert



More information about the R-help mailing list