[R] subset

Joshua Wiley jwiley.psych at gmail.com
Mon Nov 29 22:11:23 CET 2010


Hi Felipe,

On Mon, Nov 29, 2010 at 1:02 PM, Felipe Carrillo
<mazatlanmexico at yahoo.com> wrote:
>  Hi:
> I always use subset the same way but now is returning 0 rows.
> What's wrong with the way I am subsetting?
>
> library(ggplot2)
> structure(list(first = c(38.2086, 43.1768, 43.146, 41.8044, 42.4232,
> 46.3646, 38.0813, 40.0745, 40.4889, 38.6246, 40.2826, 41.6056,
> 34.5353, 40.0768), second = c(43.3295, 42.4326, 38.8994, 37.0894,
> 42.3218, 46.1726, 39.1206, 41.2072, 42.4874, 40.2657, 38.7766,
> 40.8822, 42.0165, 49.2055), third = c(42.24, 42.992, 37.7419,
> 42.3448, 41.9131, 44.385, 42.7811, 44.1963, 40.8088, 43.9634,
> 38.7079, 38.0791, 44.3136, 39.5333)), .Names = c("first", "second",
> "third"), class = "data.frame", row.names = c(NA, -14L))
>  head(x);str(x)
> xmelt <- melt(x)
>  names(xmelt) <- c("year","fatPerc")
>   str(xmelt);xmelt
>  # Subset to plot only the 'first' and third year
> firstyear <- subset(xmelt,year ==' first');str(firstyear) # works

Really?  It does not for me.  I think you need to remove the space:  '
first' to 'first'

> # two variables,,doesn't work
> firstyear <- subset(xmelt,year ==' first' & year == 'third');str(firstyear)

Same spacing problem, also I wonder if you want | instead of &.  &
will mean you are testing for years that are simultaneously the first
AND ( & ) the third, rather than the first OR ( | )the third.  See
?Logic for the distinction (in base, if it asks).

HTH,

Josh

>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
>
>
>
>
> ______________________________________________
> 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.


-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list