[R] simple subset question

David L Carlson dcarlson at tamu.edu
Sun Dec 2 23:54:16 CET 2012


As David W. guessed. The maximum is in year 2005 not 2012 so no row from
2012 matches the maximum.

> subset(winter,Year==2012 & Total==max(Total))
[1] IDWeek Total  Fry    Smolt  FryEq  Year  
<0 rows> (or 0-length row.names)
> winter[which(winter$Total==max(winter$Total)),]
    IDWeek   Total     Fry Smolt   FryEq Year
195     39 2115018 2083214 31805 2137281 2005

Change to

> subset(winter, Year==2012 & Total==max(Total[Year==2012]))
   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


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Felipe Carrillo
> Sent: Sunday, December 02, 2012 4:40 PM
> To: arun
> Cc: R help
> Subject: Re: [R] simple subset question
> 
> Actually, I dput(winter) and is not that big.....so here is the whole
> thing I
> am trying to accomplish.
>  winter <- 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, 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, 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, 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, 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, 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, 0L, 460L, 1129L, 1757L, 11105L, 27730L, 55769L,
> 12298L, 32964L, 80110L, 160507L, 74134L, 101736L, 138355L, 107873L,
> 53806L, 194429L, 7596L, 55497L, 30336L, 18392L, 22291L, 16850L,
> 16567L, 14799L, 7909L, 93L, 329L, 363L, 2762L, 5573L, 33466L,
> 96762L, 340947L, 612835L, 347972L, 526506L, 656981L, 344181L,
> 145465L, 150543L, 935599L, 32789L, 15092L, 7288L, 9889L, 6945L,
> 5138L, 6299L, 3095L, 108227L, 1191L, 0L, 0L, 132L, 227L, 2157L,
> 7558L, 16459L, 87220L, 321195L, 209299L, 110807L, 112024L, 128058L,
> 74373L, 41531L, 15111L, 21890L, 15060L, 22538L, 12443L, 11052L,
> 3907L, 2640L, 2745L, 3936L, 17593L, 0L, 0L, 10775L, 4166L, 4958L,
> 16221L, 29401L, 34951L, 33188L, 146044L, 105007L, 185297L, 159682L,
> 207537L, 140694L, 128275L, 44274L, 27079L, 18928L, 10437L, 29984L,
> 18395L, 25846L, 4573L, 31995L, 3679L, 707L, 2390L, 8860L, 24430L,
> 40885L, 72792L, 205521L, 344493L, 662973L, 526409L, 631953L,
> 850491L, 842678L, 445987L, 558152L, 332032L, 174326L, 80601L,
> 48696L, 98571L, 103563L, 149469L, 78081L, 182478L, 2158L, 16566L,
> 0L, 868L, 2044L, 4064L, 6049L, 9399L, 13304L, 45172L, 242155L,
> 476864L, 712534L, 1058409L, 2115018L, 1510342L, 1138213L, 333192L,
> 158820L, 94379L, 348882L, 39290L, 29701L, 47258L, 69837L, 7884L,
> 49338L, 22168L, 0L, 147L, 1231L, 3216L, 5021L, 18462L, 37441L,
> 64669L, 136590L, 338523L, 559448L, 714837L, 358037L, 180910L,
> 291943L, 222708L, 163801L, 39109L, 23247L, 15726L, 38701L, 5795L,
> 12509L, 18721L, 822L, 6674L, 765L, 1753L, 6226L, 9727L, 22033L,
> 50472L, 67863L, 100909L, 276699L, 239609L, 416465L, 845073L,
> 1071051L, 571656L, 409021L, 451331L, 167006L, 68069L, 250439L,
> 104641L, 76062L, 35261L, 55883L, 16064L, 11058L, 8872L, 9346L,
> 24760L, 41699L, 40032L, 80576L, 152208L, 171888L, 292862L, 522669L,
> 441518L, 858538L, 906010L, 1464311L, 1334433L, 542204L, 211514L,
> 64507L, 94830L, 100663L, 116931L, 83139L, 46268L, 14455L, 22811L,
> 47446L, 54224L), 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, 0L, 460L, 1129L, 1757L,
> 11105L, 27730L, 55769L, 12109L, 32638L, 79547L, 158892L, 72867L,
> 96249L, 130695L, 88837L, 32531L, 65746L, 1885L, 4188L, 680L,
> 100L, 109L, 0L, 0L, 0L, 0L, 93L, 329L, 363L, 2762L, 5573L, 33466L,
> 96694L, 339394L, 611967L, 345807L, 522991L, 645886L, 327353L,
> 118537L, 109158L, 419571L, 6691L, 456L, 133L, 0L, 0L, 0L, 0L,
> 0L, 0L, 0L, 0L, 0L, 132L, 227L, 2157L, 7558L, 16459L, 87220L,
> 320684L, 207921L, 110221L, 110021L, 123153L, 63829L, 23982L,
> 5090L, 4183L, 374L, 439L, 78L, 67L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
> 10775L, 4166L, 4958L, 16221L, 29401L, 34851L, 32933L, 145103L,
> 105007L, 182715L, 153781L, 201622L, 129968L, 87997L, 17235L,
> 3401L, 1343L, 397L, 316L, 37L, 0L, 0L, 0L, 0L, 707L, 2390L, 8860L,
> 24430L, 40885L, 72792L, 205521L, 343593L, 658875L, 523360L, 627333L,
> 847147L, 837189L, 429016L, 523436L, 304785L, 125005L, 28047L,
> 5141L, 6738L, 2255L, 1065L, 0L, 0L, 0L, 0L, 0L, 868L, 2044L,
> 4064L, 6049L, 9399L, 13304L, 44961L, 241575L, 474250L, 706084L,
> 1045237L, 2083214L, 1483109L, 1091328L, 284964L, 96754L, 33605L,
> 74341L, 1795L, 828L, 36L, 0L, 0L, 0L, 0L, 0L, 147L, 1231L, 3216L,
> 5021L, 18462L, 37441L, 64508L, 136280L, 337493L, 556858L, 710292L,
> 353191L, 174072L, 265099L, 166694L, 85538L, 6630L, 2033L, 623L,
> 196L, 0L, 0L, 0L, 0L, 0L, 765L, 1753L, 6226L, 9727L, 22033L,
> 50472L, 67863L, 100309L, 273113L, 237617L, 411067L, 838077L,
> 1059358L, 555461L, 364550L, 387386L, 121865L, 26477L, 25836L,
> 9169L, 2526L, 368L, 0L, 0L, 0L, 0L, 9346L, 24760L, 41699L, 40032L,
> 80576L, 152208L, 171844L, 290962L, 519078L, 438136L, 847834L,
> 891191L, 1445079L, 1298580L, 482887L, 151660L, 27477L, 20607L,
> 11141L, 2768L, 853L, 32L, 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, 0L, 0L, 0L, 0L, 0L, 0L,
> 0L, 188L, 326L, 565L, 1614L, 1268L, 5488L, 7658L, 19034L, 21275L,
> 128681L, 5710L, 51308L, 29654L, 18292L, 22183L, 16850L, 16567L,
> 14799L, 7909L, 0L, 0L, 0L, 0L, 0L, 0L, 68L, 1553L, 867L, 2164L,
> 3514L, 11094L, 16829L, 26927L, 41385L, 516027L, 26097L, 14636L,
> 7159L, 9889L, 6945L, 5138L, 6299L, 3095L, 108227L, 1191L, 0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 511L, 1378L, 585L, 2004L, 4905L,
> 10548L, 17549L, 10022L, 17709L, 14686L, 22099L, 12365L, 10985L,
> 3907L, 2640L, 2745L, 3936L, 17593L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
> 99L, 255L, 943L, 0L, 2581L, 5902L, 5916L, 10725L, 40276L, 27040L,
> 23678L, 17586L, 10042L, 29668L, 18359L, 25846L, 4573L, 31995L,
> 3679L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 900L, 4100L, 3050L, 4619L,
> 3339L, 5490L, 16971L, 34716L, 27247L, 49321L, 52554L, 43558L,
> 91836L, 101309L, 148404L, 78081L, 182478L, 2158L, 16566L, 0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 211L, 578L, 2614L, 6453L, 13177L, 31805L,
> 27233L, 46886L, 48228L, 62066L, 60773L, 274542L, 37494L, 28874L,
> 47223L, 69837L, 7884L, 49347L, 22192L, 0L, 0L, 0L, 0L, 0L, 0L,
> 0L, 160L, 308L, 1028L, 2583L, 4545L, 4847L, 6841L, 26847L, 56014L,
> 78264L, 32479L, 21216L, 15104L, 38506L, 5795L, 12509L, 18721L,
> 822L, 6674L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 601L, 3585L, 1992L,
> 5398L, 6996L, 11693L, 16197L, 44469L, 63946L, 45142L, 41592L,
> 224601L, 95474L, 73537L, 34893L, 55883L, 16064L, 11058L, 8872L,
> 0L, 0L, 0L, 0L, 0L, 0L, 44L, 1900L, 3591L, 3382L, 10704L, 14819L,
> 19232L, 35853L, 59317L, 59854L, 37030L, 74223L, 89522L, 114163L,
> 82286L, 46236L, 14455L, 22811L, 47446L, 54224L), 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, 0L, 460L, 1129L, 1757L,
> 11105L, 27730L, 55769L, 12430L, 33192L, 80505L, 161636L, 75022L,
> 105575L, 143717L, 121197L, 68698L, 284505L, 11592L, 91411L, 51093L,
> 31198L, 37820L, 28645L, 28162L, 25159L, 13442L, 93L, 329L, 363L,
> 2762L, 5573L, 33466L, 96810L, 342034L, 613438L, 349489L, 528967L,
> 664745L, 355963L, 164312L, 179515L, 1296819L, 51058L, 25338L,
> 12300L, 16808L, 11809L, 8736L, 10708L, 5263L, 163296L, 2024L,
> 0L, 0L, 132L, 227L, 2157L, 7558L, 16459L, 87220L, 321552L, 210259L,
> 111217L, 113427L, 131491L, 81757L, 53815L, 22127L, 34285L, 25340L,
> 38007L, 21098L, 18742L, 6640L, 4486L, 4668L, 6690L, 29906L, 0L,
> 0L, 10775L, 4166L, 4958L, 16221L, 29401L, 35021L, 33366L, 146703L,
> 105007L, 187104L, 163812L, 211677L, 148205L, 156467L, 63202L,
> 43653L, 31239L, 17466L, 50751L, 31248L, 43941L, 7774L, 54393L,
> 6254L, 707L, 2390L, 8860L, 24430L, 40885L, 72792L, 205521L, 345123L,
> 665842L, 528545L, 635188L, 852828L, 846522L, 457866L, 582452L,
> 351103L, 208853L, 117390L, 79187L, 162856L, 174479L, 253350L,
> 132741L, 310215L, 3671L, 28165L, 0L, 868L, 2044L, 4064L, 6049L,
> 9399L, 13304L, 45320L, 242559L, 478694L, 717049L, 1067634L, 2137281L,
> 1529403L, 1171033L, 366950L, 202266L, 136921L, 541064L, 65536L,
> 49912L, 80312L, 118725L, 13401L, 83935L, 37848L, 0L, 147L, 1231L,
> 3216L, 5021L, 18462L, 37441L, 64781L, 136805L, 339245L, 561253L,
> 718017L, 361430L, 185699L, 310734L, 261918L, 218586L, 61845L,
> 38098L, 26299L, 65656L, 9851L, 21265L, 31828L, 1395L, 11348L,
> 765L, 1753L, 6226L, 9727L, 22033L, 50472L, 67863L, 101330L, 279209L,
> 241003L, 420242L, 849967L, 1079237L, 582994L, 440150L, 496096L,
> 198606L, 97184L, 407660L, 171474L, 127537L, 59687L, 95001L, 27311L,
> 18797L, 15080L, 9346L, 24760L, 41699L, 40032L, 80576L, 152208L,
> 171920L, 294192L, 525182L, 443886L, 866027L, 916377L, 1477769L,
> 1359528L, 583725L, 253410L, 90428L, 146786L, 163327L, 196846L,
> 140743L, 78635L, 24575L, 39309L, 81900L, 93600L), 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, 2010L, 2010L, 2010L,
> 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
> 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
> 2010L, 2010L, 2010L, 2010L, 2010L, 2009L, 2009L, 2009L, 2009L,
> 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L,
> 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L,
> 2009L, 2009L, 2009L, 2009L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
> 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
> 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
> 2007L, 2007L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L,
> 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L,
> 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L,
> 2006L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L,
> 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L,
> 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L,
> 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L,
> 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L,
> 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2003L,
> 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L,
> 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L,
> 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2002L, 2002L,
> 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L,
> 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L,
> 2002L, 2002L, 2002L, 2002L, 2002L, 2002L)), .Names = c("IDWeek",
> "Total", "Fry", "Smolt", "FryEq", "Year"), class = "data.frame",
> row.names = c(NA,
> -286L))
>  library(ggplot2)
>  library(plyr)
>  dim(winter);head(winter)
>    str(winter)
>  test <- ggplot(winter,aes(IDWeek,Total)) + geom_line(colour='blue') +
> facet_wrap(~Year,as.table=F) +
>   geom_vline(xintercept=47,colour='red',linetype='dashed')  +
> 
> geom_hline(yintercept=300000,colour='orange',linetype='dashed',size=1)
> +
>    opts(title="Winter Chinook 2002-2012")  +
>    geom_point(data=subset(winter,Year==2012 &
> Total==max(Total)),size=7,shape=1,color='green') # Want to be able to
> subset using max on the fly
>    test
> # But it doesn't subset at all
> 
>    # It works if I get the max Total like this:
>    test <- ggplot(winter,aes(IDWeek,Total)) + geom_line(colour='blue')
> + facet_wrap(~Year,as.table=F) +
>   geom_vline(xintercept=47,colour='red',linetype='dashed')  +
> 
> geom_hline(yintercept=300000,colour='orange',linetype='dashed',size=1)
> +
>    opts(title="Winter Chinook 2002-2012")  +
>    geom_point(data=subset(winter,Year==2012 & Total >
> 300000),size=7,shape=1,color='green') # it works if get the Total like
> this
>    test
> 
>     # Zooming in to the weeks of interest (weeks 45 to 52) and put a
> point on the 2012 max catch which is week 37
>     windows()
>   test + coord_cartesian(xlim=c(45,52)) +
> scale_x_continuous(breaks=c(45,47,49,51)) +
>   scale_y_continuous(limits=c(0,350000)) + opts(title="Zooming in to
> weeks 45 to 52")
> 
> 
> 
> 
> 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: arun <smartpink111 at yahoo.com>
> >To: Felipe Carrillo <mazatlanmexico at yahoo.com>
> >Cc: William Dunlap <wdunlap at tibco.com>; David Winsemius
> <dwinsemius at comcast.net>; R help <r-help at r-project.org>
> >Sent: Sunday, December 2, 2012 1:18 PM
> >Subject: Re: [R] simple subset question
> >
> >Hi,
> >From the ddply() output, you could get the whole row by:
> >
> > fish1 <- structure(list(Year = 2002:2012, maxTotal = c(1464311L,
> 1071051L,
> >714837L, 2115018L, 850491L, 207537L, 321195L, 935599L, 194429L,
> >157260L, 303259L)), .Names = c("Year", "maxTotal"), row.names = c(NA,
> >-11L), class = "data.frame")
> >
> >
> > fish[fish[,2]%in%fish1[,2][fish1[,1]==2012],]  #fish (or winter) is
> your original dataset
> >#   IDWeek  Total   Fry  Smolt  FryEq Year
> >#21     47 303259 34008 269248 491733 2012
> >A.K.
> >
> >
> >
> >
> >
> >________________________________
> >From: Felipe Carrillo <mazatlanmexico at yahoo.com>
> >To: William Dunlap <wdunlap at tibco.com>; arun <smartpink111 at yahoo.com>
> >Cc: R help <r-help at r-project.org>
> >Sent: Sunday, December 2, 2012 2:34 PM
> >Subject: Re: [R] simple subset question
> >
> >
> >
> >Using my whole dataset I get:
> >library(plyr)
> >ddply(winter,"Year",summarise,maxTotal=max(Total))
> >
> > fish <- structure(list(Year = 2002:2012, maxTotal = c(1464311L,
> 1071051L,
> >714837L, 2115018L, 850491L, 207537L, 321195L, 935599L, 194429L,
> >157260L, 303259L)), .Names = c("Year", "maxTotal"), row.names = c(NA,
> >-11L), class = "data.frame")
> >
> >I only want to extract the max Total for 2012 and want the whole row
> like this:
> > IDWeek  Total   Fry  Smolt  FryEq Year
> >21     47 303259 34008 269248 491733 2012
> >
> >My whole dataset is too big to post it so thanks for your help and
> will try
> >to figure out why subset returns an empty row
> >
> >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: William Dunlap <wdunlap at tibco.com>
> >>To: Felipe Carrillo <mazatlanmexico at yahoo.com>; arun
> <smartpink111 at yahoo.com>
> >>Cc: R help <r-help at r-project.org>
> >>Sent: Sunday, December 2, 2012 11:00 AM
> >>Subject: RE: [R] simple subset question
> >>
> >>> 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)
> >>
> >>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?
> >>
> >>> tmp <- transform(fish, YearlyMaxTotal = ave(Total, Year, FUN=max))
> >>> subset(tmp, Total==YearlyMaxTotal)
> >>  IDWeek  Total    Fry  Smolt  FryEq Year YearlyMaxTotal
> >>21    47 303259  34008 269248 491733 2012        303259
> >>39    39 157260 156909    351 157506 2011        157260
> >>> subset(tmp, Total==YearlyMaxTotal
> >& Year==2012)
> >>  IDWeek  Total  Fry  Smolt  FryEq Year YearlyMaxTotal
> >>21    47 303259 34008 269248 491733 2012        303259
> >>
> >>Bill Dunlap
> >>Spotfire, TIBCO Software
> >>wdunlap tibco.com
> >>
> >>
> >>> -----Original Message-----
> >>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf
> >>> Of Felipe Carrillo
> >>> Sent: Sunday, December 02, 2012 10:47 AM
> >>> To: arun
> >>> Cc: R help
> >>> Subject: Re: [R] simple subset question
> >>>
> >>> Works with the small dataset (2 years) but I get the error message
> with the whole
> >>> dataset (12 years of data). I am going to have
> >>> to check what's
> >wrong with it...Thanks
> >>>
> >>> 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: arun <smartpink111 at yahoo.com>
> >>> >To: Felipe Carrillo <mazatlanmexico at yahoo.com>
> >>> >Cc: R help <r-help at r-project.org>; R. Michael Weylandt
> >>> <michael.weylandt at gmail.com>
> >>> >Sent: Sunday, December 2, 2012 10:29 AM
> >>> >Subject: Re: [R] simple subset question
> >>> >
> >>>
> >>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>
> >>> >>To: Felipe Carrillo <mazatlanmexico at yahoo.com>
> >>> >>Cc: "r-help at r-project.org" <r-help at r-project.org>
> >>> >>Sent: Sunday, December 2, 2012 9:42 AM
> >>> >>Subject: Re: [R] simple subset question
> >>> >>
> >>> >>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.
> >>> >>>
> >>> >>
> >>> >>
> >>> >>
> >>> >    [[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.
> >>> >
> >>> >
> >>> >
> >>> >
> >>>     [[alternative HTML version deleted]]
> >>
> >>
> >>
> >>
> >
> >
> >
> 	[[alternative HTML version deleted]]




More information about the R-help mailing list