[R] simple subset question
R. Michael Weylandt
michael.weylandt at gmail.com
Sun Dec 2 18:42:23 CET 2012
On Sun, Dec 2, 2012 at 5:21 PM, Felipe Carrillo
<mazatlanmexico at yahoo.com> wrote:
> Hi,
> Consider the small dataset below, I want to subset by two variables in
> one line but it wont work...it works though if I subset separately. I have
> to be missing something obvious that I did not realize before while using subset..
>
> fish <- structure(list(IDWeek = c(27L, 28L, 29L, 30L, 31L, 32L, 33L,
> 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
> 47L, 48L, 49L, 50L, 51L, 52L, 27L, 28L, 29L, 30L, 31L, 32L, 33L,
> 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
> 47L, 48L, 49L, 50L, 51L, 52L), Total = c(0L, 0L, 326L, 1735L,
> 1807L, 2208L, 3883L, 8820L, 6060L, 19326L, 63158L, 100718L, 53015L,
> 91689L, 152629L, 122708L, 61293L, 15574L, 86538L, 75365L, 303259L,
> 19691L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 161L, 321L, 1000L, 4425L,
> 13202L, 19726L, 30518L, 84949L, 157260L, 145691L, 85801L, 62044L,
> 44439L, 23272L, 22391L, 20159L, 14854L, 35379L, 31142L, 7736L,
> 13221L, 4894L), Fry = c(0L, 0L, 326L, 1735L, 1807L, 2208L, 3883L,
> 8759L, 6060L, 19326L, 63119L, 100524L, 52582L, 88170L, 145564L,
> 111416L, 38233L, 5248L, 17826L, 11038L, 34008L, 215L, 0L, 0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 161L, 321L, 1000L, 4425L, 13055L, 19488L,
> 30518L, 84818L, 156909L, 144786L, 84207L, 57720L, 31049L, 6858L,
> 1616L, 719L, 364L, 49L, 0L, 0L, 0L, 0L), Smolt = c(0L, 0L, 0L,
> 0L, 0L, 0L, 0L, 62L, 0L, 0L, 38L, 195L, 433L, 3518L, 7067L, 11290L,
> 23058L, 10327L, 68712L, 64328L, 269248L, 19479L, 0L, 0L, 0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 147L, 238L, 0L, 131L, 351L,
> 905L, 1592L, 4324L, 13391L, 16414L, 20774L, 19444L, 14491L, 35330L,
> 31142L, 7736L, 13221L, 4894L), FryEq = c(0L, 0L, 326L, 1735L,
> 1807L, 2208L, 3883L, 8864L, 6060L, 19326L, 63185L, 100854L, 53318L,
> 94151L, 157576L, 130610L, 77432L, 22805L, 134639L, 120393L, 491733L,
> 33327L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 161L, 321L, 1000L, 4425L,
> 13306L, 19894L, 30518L, 85042L, 157506L, 146328L, 86914L, 65073L,
> 53812L, 34763L, 36931L, 33769L, 24998L, 60110L, 52938L, 13149L,
> 22476L, 8319L), Year = c(2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
> 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
> 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
> 2012L, 2012L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
> 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
> 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
> 2011L)), .Names = c("IDWeek", "Total", "Fry", "Smolt", "FryEq",
> "Year"), row.names = c(NA, 52L), class = "data.frame")
> fish
> # Subset to get the max Total for 2012
> x <- subset(winter,Year==2012 & Total==max(Total));b # How come one line doesn't work?
Works fine for me if I change "winter" to fish here.
subset(fish,Year==2012 & Total==max(Total))
IDWeek Total Fry Smolt FryEq Year
21 47 303259 34008 269248 491733 2012
>
> # It works if I subset the year first and then get the Total max from it
> xx <- subset(winter,Year==2012)
> xxx <- subset(xx,Total==max(Total));xxx
> xxx
>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
> http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>
> [[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.
>
More information about the R-help
mailing list