[R] select row based on highest value

jim holtman jholtman at gmail.com
Tue Feb 23 22:58:54 CET 2010


Is this better:

> sapply(split(v, v$Prop), function(x) x$TAZ[which.max(x$area)])
p754921  p75506  p75508
     37     282      46
>


Take a look at what you were doing.  The result of the 'tapply' is to
split v$area in to smaller groups based on v$Prop, so the 'x'
parameter is not going to be the same length as your 'v' dataframe and
therefore you are indexing to the incorrect value.  Best to split the
dataframe and then operate on the individual portions.

On Tue, Feb 23, 2010 at 3:47 PM, LCOG1 <jroll at lcog.org> wrote:
>
> Please consider the following
>
> #Data to use
>  Props<-c("p754921","p754921" ,"p754921","p75506" ,"p75506"
> ,"p75506","p75506","p75508","p75508","p75508","p75508","p75508")
>  TAZ<-c(38,37,37,171,171,282,171,46,46,169,169,169)
>  Area<-c(109828.04,  128134.71,   46469.57, 37160.21,
>  40080.50,344679.66,16972.28,
>  342309.558, 260906.870,  17014.659,   7285.706,  10936.316)
>
>  TazProperties..<-data.frame(Props,TAZ,Area)
>
>
> t <- TazProperties..
> v <- aggregate(t$Area, by=list(t$TAZ,t$Props), sum)
> names(v) <- c("TAZ", "Prop", "area")
> a<-tapply(v$area, v$Prop, function(x) v$TAZ[which.max(x)])
>
> "a" returns:
>
> p754921  p75506  p75508
>     37      38      37
>
> and it should return
>
> p754921  p75506  p75508
>     37      282      46
>
> What am i doing wrong?  Thanks
> --
> View this message in context: http://n4.nabble.com/select-row-based-on-highest-value-tp1566510p1566510.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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list