dear R experts---I am trying to figure out what the recommended way is to merge by() results back into the original data frame. for example, I want to have a flag that tells me whether a particular row contains the maximum for its group. d <- data.frame(group=as.factor(rep(1:3,each=3)), val=rnorm(9)) highestvals <- by( d, d$group, function(d)(max(d$val)) ) ## and now? iterate over levels( d$group ) ? how do I merge highestvals back into d? advice appreciated. sincerely, /iaw ---- Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.com)
Thomas Lumley
2011-Mar-21 23:09 UTC
[R] Merging by() results back into original data frame?
On Tue, Mar 22, 2011 at 11:42 AM, ivo welch <ivowel at gmail.com> wrote:> dear R experts---I am trying to figure out what the recommended way is > to merge by() results back into the original data frame. ?for example, > I want to have a flag that tells me whether a particular row contains > the maximum for its group. > > ?d <- data.frame(group=as.factor(rep(1:3,each=3)), val=rnorm(9)) > ?highestvals <- by( d, d$group, function(d)(max(d$val)) ) > > ?## and now? ?iterate over levels( d$group ) ? ?how do I merge > highestvals back into d?The easiest approach is probably to use ave() instead of by(). Otherwise match() will tell you where to put each number. -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland
William Dunlap
2011-Mar-21 23:09 UTC
[R] Merging by() results back into original data frame?
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of ivo welch > Sent: Monday, March 21, 2011 3:43 PM > To: r-help > Subject: [R] Merging by() results back into original data frame? > > dear R experts---I am trying to figure out what the recommended way is > to merge by() results back into the original data frame. for example, > I want to have a flag that tells me whether a particular row contains > the maximum for its group. > > d <- data.frame(group=as.factor(rep(1:3,each=3)), val=rnorm(9))ave() could do what you want without using by(). E.g., > d$isGroupMax <- with(d, ave(val, group, FUN=max) == val) > d group val isGroupMax 1 1 0.21496662 FALSE 2 1 -1.44767939 FALSE 3 1 0.39635971 TRUE 4 2 0.60235172 FALSE 5 2 0.94581401 TRUE 6 2 0.01665084 FALSE 7 3 -0.58277312 FALSE 8 3 0.82930370 FALSE 9 3 1.02906920 TRUE Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> highestvals <- by( d, d$group, function(d)(max(d$val)) ) > > ## and now? iterate over levels( d$group ) ? how do I merge > highestvals back into d? > > advice appreciated. > > sincerely, > > /iaw > ---- > Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.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. >
thank you, william and bill. wow, this was fast. I have been tearing my hair out over this one, trying to work the wrong tool. (this would make a good "see also" in the "by" function.) ---- Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.com) On Mon, Mar 21, 2011 at 7:09 PM, William Dunlap <wdunlap at tibco.com> wrote:>> -----Original Message----- >> From: r-help-bounces at r-project.org >> [mailto:r-help-bounces at r-project.org] On Behalf Of ivo welch >> Sent: Monday, March 21, 2011 3:43 PM >> To: r-help >> Subject: [R] Merging by() results back into original data frame? >> >> dear R experts---I am trying to figure out what the recommended way is >> to merge by() results back into the original data frame. ?for example, >> I want to have a flag that tells me whether a particular row contains >> the maximum for its group. >> >> ? d <- data.frame(group=as.factor(rep(1:3,each=3)), val=rnorm(9)) > > ave() could do what you want without using by(). ?E.g., > > ?> d$isGroupMax <- with(d, ave(val, group, FUN=max) == val) > ?> d > ? group ? ? ? ? val isGroupMax > ?1 ? ? 1 ?0.21496662 ? ? ?FALSE > ?2 ? ? 1 -1.44767939 ? ? ?FALSE > ?3 ? ? 1 ?0.39635971 ? ? ? TRUE > ?4 ? ? 2 ?0.60235172 ? ? ?FALSE > ?5 ? ? 2 ?0.94581401 ? ? ? TRUE > ?6 ? ? 2 ?0.01665084 ? ? ?FALSE > ?7 ? ? 3 -0.58277312 ? ? ?FALSE > ?8 ? ? 3 ?0.82930370 ? ? ?FALSE > ?9 ? ? 3 ?1.02906920 ? ? ? TRUE > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > >> ? highestvals <- by( d, d$group, function(d)(max(d$val)) ) >> >> ? ## and now? ?iterate over levels( d$group ) ? ?how do I merge >> highestvals back into d? >> >> advice appreciated. >> >> sincerely, >> >> /iaw >> ---- >> Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.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. >> >
baptiste auguie
2011-Mar-21 23:13 UTC
[R] Merging by() results back into original data frame?
I find it quite neat with plyr, library(plyr) ddply(d, .(group), transform, max=max(val)) HTH, baptiste On 22 March 2011 12:09, William Dunlap <wdunlap at tibco.com> wrote:>> -----Original Message----- >> From: r-help-bounces at r-project.org >> [mailto:r-help-bounces at r-project.org] On Behalf Of ivo welch >> Sent: Monday, March 21, 2011 3:43 PM >> To: r-help >> Subject: [R] Merging by() results back into original data frame? >> >> dear R experts---I am trying to figure out what the recommended way is >> to merge by() results back into the original data frame. ?for example, >> I want to have a flag that tells me whether a particular row contains >> the maximum for its group. >> >> ? d <- data.frame(group=as.factor(rep(1:3,each=3)), val=rnorm(9)) > > ave() could do what you want without using by(). ?E.g., > > ?> d$isGroupMax <- with(d, ave(val, group, FUN=max) == val) > ?> d > ? group ? ? ? ? val isGroupMax > ?1 ? ? 1 ?0.21496662 ? ? ?FALSE > ?2 ? ? 1 -1.44767939 ? ? ?FALSE > ?3 ? ? 1 ?0.39635971 ? ? ? TRUE > ?4 ? ? 2 ?0.60235172 ? ? ?FALSE > ?5 ? ? 2 ?0.94581401 ? ? ? TRUE > ?6 ? ? 2 ?0.01665084 ? ? ?FALSE > ?7 ? ? 3 -0.58277312 ? ? ?FALSE > ?8 ? ? 3 ?0.82930370 ? ? ?FALSE > ?9 ? ? 3 ?1.02906920 ? ? ? TRUE > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > >> ? highestvals <- by( d, d$group, function(d)(max(d$val)) ) >> >> ? ## and now? ?iterate over levels( d$group ) ? ?how do I merge >> highestvals back into d? >> >> advice appreciated. >> >> sincerely, >> >> /iaw >> ---- >> Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.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. >> > > ______________________________________________ > 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. >