Nick Ackerman
2010-Mar-17 17:00 UTC
[R] How can I return rows from a data frame with maximum value by factor?
Hi, I'm new to R and new to this forum. I'm struggling with trying to extract certain rows of data from my data.frame. The data.frame has eleven columns. Among those columns are "FISH_ID" and "DATE_TIME". FISH_ID is a factor. For each of my 21 unique FISH_IDs (levels) I have a few to a few thousand rows, each row with a unique DATE_TIME value. I would like to obtain, for each FISH_ID, the entire record from the data.frame with the latest DATE_TIME value (i.e. max (DATE_TIME). Thus, the resulting subset would be the original eleven columns by 21 rows (one for each level of my FISH_ID). Any advice would be greatly appreciated. Nick -- View this message in context: http://n4.nabble.com/How-can-I-return-rows-from-a-data-frame-with-maximum-value-by-factor-tp1596777p1596777.html Sent from the R help mailing list archive at Nabble.com.
Nick Ackerman
2010-Mar-17 17:44 UTC
[R] How can I return rows from a data frame with maximum value by factor?
Solved my own question by looking at some other threads. For those who may be intersted, here was the solution to my problem. do.call(rbind,lapply(split(data.frame,FISH_ID),function(x)x[which.max(x$DATE_TIME),])) -- View this message in context: http://n4.nabble.com/How-can-I-return-rows-from-a-data-frame-with-maximum-value-by-factor-tp1596777p1596853.html Sent from the R help mailing list archive at Nabble.com.
Greg Snow
2010-Mar-17 20:57 UTC
[R] How can I return rows from a data frame with maximum value by factor?
For future reference you may want to look at the plyr package, or even the sapply and aggregate functions and possibly simpler ways to do what you did below. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Nick Ackerman > Sent: Wednesday, March 17, 2010 11:45 AM > To: r-help at r-project.org > Subject: Re: [R] How can I return rows from a data frame with maximum > value by factor? > > > Solved my own question by looking at some other threads. For those who > may be > intersted, here was the solution to my problem. > > do.call(rbind,lapply(split(data.frame,FISH_ID),function(x)x[which.max(x > $DATE_TIME),])) > -- > View this message in context: http://n4.nabble.com/How-can-I-return- > rows-from-a-data-frame-with-maximum-value-by-factor- > tp1596777p1596853.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.