[R] selecting maximum values
Roger Bivand
Roger.Bivand at nhh.no
Wed May 4 19:35:47 CEST 2005
On Wed, 4 May 2005, Sean Davis wrote:
> see ?aggregate.
Or maybe tapply, or its close relative, by:
> by(df, list(df$station, df$date), function(x)
+ x$row[which.max(x$chlorophyll)])
: Ancona
: 21/06/01
[1] NA
------------------------------------------------------------
: Castagneto
: 21/06/01
[1] 3
------------------------------------------------------------
: Ancona
: 23/06/01
[1] 6
------------------------------------------------------------
: Castagneto
: 23/06/01
[1] NA
since happily a row ID column was included in the data frame. Note that
which.max only reports the row of the first maximum if there are ties.
>
> Sean
>
> On May 4, 2005, at 11:43 AM, alessandro carletti wrote:
>
> > Sorry for disturbing you with another newbie question!
> > I have a data frame about coastal waters quality
> > parameters: for some parameters (e.g. NH3) I have only
> > 1 observation for each sampling station and each
> > sampling date, while in other cases (chlorophyll) I
> > have 1 obs for each meter-depth for each station and
> > date. How can I select only the max chlorophyll value
> > for each station/date?
> >
> > example
> >
> > row station date depth chlorophyll
> > 1 Castagneto 21/06/01 -0.5 2.0
> > 2 Castagneto 21/06/01 -1.5 2.2
> > 3 Castagneto 21/06/01 -2.5 2.4
> > 4 Castagneto 21/06/01 -3.5 2.1
> > 5 Ancona 23/06/01 -0.5 2.4
> > 6 Ancona 23/06/01 -1.5 2.5
> > 7 Ancona 23/06/01 -2.5 2.2
> > 8 Ancona 23/06/01 -3.5 2.1
> > 9 Ancona 23/06/01 -4.5 1.9
> > ...
> >
> > I'd like to select only row 3 and 6, the ones with max
> > chlorophyll values, or have the mean for the rows 1:4
> > and 5:9
> >
> > Thanks
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-help
mailing list