Skip to content

simple subset question

17 messages · Anthony Damico, Gerrit Eichner, Felipe Carrillo +5 more

#
On Sun, Dec 2, 2012 at 5:21 PM, Felipe Carrillo
<mazatlanmexico at yahoo.com> wrote:
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
#
On Dec 2, 2012, at 9:21 AM, Felipe Carrillo wrote:

            
Try instead either of these one step operations:
 > xxx <- max( subset(fish, Year==2012 )$Total) ;xxx
[1] 303259
 > xxx <- max( subset(fish, Year==2012 , Total) ) ;xxx
[1] 303259
#
Hi, Felipe,

two typos? See below!
On Sun, 2 Dec 2012, Felipe Carrillo wrote:

            
Don't you want *fish* instead of *winter* and *x* instead of *b*, as in

x <- subset(  fish    ,Year==2012 & Total==max(Total));    x

?

  Hth  --  Gerrit
#
Hi,
I am getting this:
x<-subset(fish,Year==2012 & Total==max(Total))
?x
#?? IDWeek? Total?? Fry? Smolt? FryEq Year
#21???? 47 303259 34008 269248 491733 2012
A.K.




----- Original Message -----
From: Felipe Carrillo <mazatlanmexico at yahoo.com>
To: R. Michael Weylandt <michael.weylandt at gmail.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Sunday, December 2, 2012 1:25 PM
Subject: Re: [R] simple subset question

Sorry, I was trying it to subset from a bigger dataset called 'winter' and forgot to change the variable names
when I asked the question. David W suggestion works but the strange part is that I am still getting an error message
with :
? x <- subset(fish,Year==2012 & Total==max(Total));x
I get:
[1] IDWeek Total? Fry??? Smolt? FryEq? Year? 
<0 rows> (or 0-length row.names)
?
I will start a fresh session to see if that helps...Thank you all

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
http://www.fws.gov/redbluff/rbdd_jsmp.aspx


From: R. Michael Weylandt <michael.weylandt at gmail.com>
??? [[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.
#
On Sun, Dec 2, 2012 at 6:46 PM, Felipe Carrillo
<mazatlanmexico at yahoo.com> wrote:
Off the cuff guess: there's a NA in Total so max(Total) is NA which
throws a wrench in `==`.

MW
#
The above is not an error message.  It says that there
are no rows satisfying your criteria.  Note that Total==max(Total)
returns a TRUE for each row in which the Total value
equals the maximum Total value over all the years in
the data.  Are you looking for the maximum value of Total
in each year?
IDWeek  Total    Fry  Smolt  FryEq Year YearlyMaxTotal
21     47 303259  34008 269248 491733 2012         303259
39     39 157260 156909    351 157506 2011         157260
IDWeek  Total   Fry  Smolt  FryEq Year YearlyMaxTotal
21     47 303259 34008 269248 491733 2012         303259

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
The reason I suggested the alternative that I did was because your  
code could fail when the max(Total) value was not in the subset where  
Year==2012.
#
As David W. guessed. The maximum is in year 2005 not 2012 so no row from
2012 matches the maximum.
[1] IDWeek Total  Fry    Smolt  FryEq  Year  
<0 rows> (or 0-length row.names)
IDWeek   Total     Fry Smolt   FryEq Year
195     39 2115018 2083214 31805 2137281 2005

Change to
IDWeek  Total   Fry  Smolt  FryEq Year
21     47 303259 34008 269248 491733 2012

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352