Re: [R] using subset() in data frame

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Sat, 23 Feb 2008 11:11:17 -0700

Look carefully at the output and commands below.  

The first level of porosity is "macro " (notice the space at the end) but you are asking for "macro" (without the space). Computers are very literal, so "macro " is not equal to "macro". Try it again with the space (or renaming the level in the factor to not include the space). Also check to see if the name of porosity has a space or something in it (the $ method will do a partial match so data.b$porosity will match if the factor is named "porosity" or "porosity " or "porosity.somethingelseafterthemainname", but the subset command is only looking for something named "porosity" and will not match the others).  

Hope this helps,


From: r-help-bounces_at_r-project.org on behalf of Robert Walters Sent: Sat 2/23/2008 7:13 AM
To: Chuck Cleland
Cc: r-help_at_r-project.org
Subject: Re: [R] using subset() in data frame

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



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.

        [[alternative HTML version deleted]]



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 - 18:16:05 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 - 19:30:17 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