Re: [R] Merging by() results back into original data frame?

From: Thomas Lumley <tlumley_at_uw.edu>
Date: Tue, 22 Mar 2011 12:09:28 +1300

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

______________________________________________
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.
Received on Mon 21 Mar 2011 - 23:13:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Mar 2011 - 23:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive