Rawlins, Barry G.
2011-Sep-13 16:31 UTC
[R] Selecting row indices from a data.frame by a factor and simple calculation (miniumum)
Hello I wish to extract the row indices from a data.frame in which a column contains numeric data by calculating the minimum value, but grouped on another column factor: An example data.frame: Code absdiff NY14/3070 2 NY14/3070 4 NY14/3070 1 NY14/5459 5 NY14/5459 7 So in this case the factor is Code and the numeric vector is absdiff The query would return row indices 3 and 4 which have the minimum values for the factor. I am sure it would involve some combination of which and tapply but cannot figure it out? Many thanks, Barry Dr Barry Rawlins British Geological Survey Keyworth Nottingham NG12 5GG UK Tel: 0115 9363140 Mob: 07884235473 homepage http://sites.google.com/site/barryrawlins/ -- This message (and any attachments) is for the recipient ...{{dropped:8}}
Mikkel Grum
2011-Sep-13 17:18 UTC
[R] Selecting row indices from a data.frame by a factor and simple calculation (miniumum)
Code <- c(rep("NY14/3070", 3), rep("NY14/5459", 2)) Code <- as.factor(Code) absdiff <- c(2, 4, 1, 5, 7) df <- data.frame(Code, absdiff) which( ? ? paste(df$Code, df$absdiff) ==? ? ? paste( ? ? ? ? aggregate(df$absdiff, by = list(df$Code), min)$Group.1, ? ? ? ? aggregate(df$absdiff, by = list(df$Code), min)$x ? ? ) ) ----- Original Message ----- From: "Rawlins, Barry G." <bgr at bgs.ac.uk> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, September 13, 2011 11:31 AM Subject: [R] Selecting row indices from a data.frame by a factor and simple calculation (miniumum) Hello I wish to extract the row indices from a data.frame in which a column contains numeric data by calculating the minimum value, but grouped on another column factor: An example data.frame: Code? ? ? ? ? ? ? ? absdiff NY14/3070? ? ? ? 2 NY14/3070? ? ? ? 4 NY14/3070? ? ? ? 1 NY14/5459? ? ? ? 5 NY14/5459? ? ? ? 7 So in this case the factor is Code and the numeric vector is absdiff The query would return row indices 3 and 4 which have the minimum values for the factor. I am sure it would involve some combination of which and tapply but cannot figure it out? Many thanks, Barry Dr Barry Rawlins British Geological Survey Keyworth Nottingham NG12 5GG UK Tel: 0115 9363140 Mob: 07884235473 homepage http://sites.google.com/site/barryrawlins/ -- This message (and any attachments) is for the recipient ...{{dropped:8}} ______________________________________________ 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.
David Winsemius
2011-Sep-13 18:41 UTC
[R] Selecting row indices from a data.frame by a factor and simple calculation (miniumum)
On Sep 13, 2011, at 12:31 PM, Rawlins, Barry G. wrote:> Hello > > I wish to extract the row indices from a data.frame in which a > column contains numeric data by calculating the minimum value, but > grouped on another column factor: > > An example data.frame: > > Code absdiff > NY14/3070 2 > NY14/3070 4 > NY14/3070 1 > NY14/5459 5 > NY14/5459 7 > > So in this case the factor is Code and the numeric vector is absdiff> df$grp.min <- ave(df$absdiff, df$Code, FUN=min) > rownames(df[df$absdiff ==df$grp.min,]) [1] "3" "4" Or if you didn't want that column clutterng up your data.frame it could be just aclulated within hte loggical comparison: > rownames(df[ df$absdiff ==ave(df$absdiff, df$Code, FUN=min), ]) [1] "3" "4" Now it should be noted that if there are multiple values of the group minimum, then they will be returned.> > The query would return row indices 3 and 4 which have the minimum > values for the factor. > I am sure it would involve some combination of which and tapply but > cannot figure it out? > > Many thanks, Barry > > Dr Barry Rawlins > British Geological SurveyDavid Winsemius, MD West Hartford, CT