[R-sig-Geo] simple help to calculate max statistic in data.frame

Torleif Markussen Lunde torleif.lunde at cih.uib.no
Sun Mar 8 19:55:36 CET 2009


Hi Gianni

To simplify things you could use tapply()

data.df <- data.frame(ID=c(1,1,1, 4,4,4,5,5,5,5,5,5), pH=c(3.4, 3.8, 3.9, 5.5, 
5.9, 5.8, 7.1, 7.5, 7.6, 7.7, 7.8, 7.9))

with(data.df, tapply(pH, ID, max))

The solution of Tom could of course also be used.

Best wishes
Torleif


On Sunday 08 March 2009 07:26:00 pm Tom Gottfried wrote:
> Hi Gianni,
>
> when you combine the information given in chapter 2.7 (especially the
> part about logical index vectors) and chapter 5.2 (note that a
> data.frame here acts like an array with two dimensions) of "An
> Introduction to R", you see that you can do that with logical index
> vectors (e.g. looping over the values in your column "ID").
> Furthermore you can consider using `subset()'.
>
> Hope this helps.
>
> Regards,
> Tom
>
> > Date: Fri, 6 Mar 2009 13:08:25 +0100
> > From: gianni lavaredo <gianni.lavaredo at gmail.com>
> > Subject: [R-sig-Geo] simple help to calculate max statistic in
> > 	data.frame
> > To: r-sig-geo at stat.math.ethz.ch
> > Message-ID:
> > 	<518dff330903060408u660639c0w334c57442efb393c at mail.gmail.com>
> > Content-Type: text/plain
> >
> > Thanks for help and attention
> >
> > I have a data.frame "sample" in shape-file like this
> >
> > ID; pH
> > 1;3.4
> > 1;3.8
> > 1;3.9
> > 4;5.5
> > 4;5.9
> > 4;5.8
> > 5;7.1
> > 5;7.5
> > 5;7.6
> > 5;7.7
> > 5;7.8
> > 5;7.9
> >
> > I wish to extract the max(pH) of ID = 1, after max(pH) of ID = 2,
> > etc. etc.
> >
> > I tryed the code
> >
> > sample <- readOGR(".", "sample")
> > max(sample$pH) #to obtain the max of pH
> > max(sample$ph[c(1), ] #not run
> >
> > thanks
> > Gianni
> >
> > 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list