Hi all I have a very basic question, yet i have not found how to do it. Suppose my dataset looks like this: Year Area value 1 a 20 1 a 25 1 a 28 1 a 31 1 a 23 1 b 25 1 b 28 1 b 23 1 b 19 2 a 25 2 a 23 2 a 24 2 a 26 2 b 27 2 b 28 2 b 20 2 b 25 2 b 28 Now, i want to calculate a MEAN per year per area. How do i do that? With mean(value) i calculate the mean of all values of course. I just need to know how to group year and area correctly. I assume that i can use this grouping in other calculations too, right? Cheers, Luc -- View this message in context: http://www.nabble.com/calculating-means-per-group-tp19271479p19271479.html Sent from the R devel mailing list archive at Nabble.com.
Dear Luc, You should send this kind of questions to the general mailing list (r-help at r-project.org) instead of the developer list. To answer your question: have a look at ?by and ?aggregate HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] Namens RFTW Verzonden: donderdag 4 september 2008 9:07 Aan: r-devel at r-project.org Onderwerp: [Rd] calculating means per group Hi all I have a very basic question, yet i have not found how to do it. Suppose my dataset looks like this: Year Area value 1 a 20 1 a 25 1 a 28 1 a 31 1 a 23 1 b 25 1 b 28 1 b 23 1 b 19 2 a 25 2 a 23 2 a 24 2 a 26 2 b 27 2 b 28 2 b 20 2 b 25 2 b 28 Now, i want to calculate a MEAN per year per area. How do i do that? With mean(value) i calculate the mean of all values of course. I just need to know how to group year and area correctly. I assume that i can use this grouping in other calculations too, right? Cheers, Luc -- View this message in context: http://www.nabble.com/calculating-means-per-group-tp19271479p19271479.ht ml Sent from the R devel mailing list archive at Nabble.com. ______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.%CRLF%The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document%CRLF%
Hi Luc, First of all, questions like this should really be asked on the R-help mailing list. The tapply function does what you want: > year [1] 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 Levels: 1 2 > area [1] a a a a a b b b b a a a a b b b b b Levels: a b > value [1] 20 25 28 31 23 25 28 23 19 25 23 24 26 27 28 20 25 28 Note that both year and area are factors. Get the mean for each area: > tapply(value, area, mean) a b 25.00000 24.77778 If you make the second argument a list then you can subset on both factor columns: > tapply(value, list(year, area), mean) a b 1 25.4 23.75 2 24.5 25.60 Kjell On 4 sept. 08, at 09:06, RFTW wrote:> > Hi all > I have a very basic question, yet i have not found how to do it. > > Suppose my dataset looks like this: > > Year Area value > 1 a 20 > 1 a 25 > 1 a 28 > 1 a 31 > 1 a 23 > 1 b 25 > 1 b 28 > 1 b 23 > 1 b 19 > 2 a 25 > 2 a 23 > 2 a 24 > 2 a 26 > 2 b 27 > 2 b 28 > 2 b 20 > 2 b 25 > 2 b 28 > > > Now, i want to calculate a MEAN per year per area. How do i do that? > > With mean(value) i calculate the mean of all values of course. I > just need > to know how to group year and area correctly. > > I assume that i can use this grouping in other calculations too, > right? > > > Cheers, > > Luc > -- > View this message in context: http://www.nabble.com/calculating-means-per-group-tp19271479p19271479.html > Sent from the R devel mailing list archive at Nabble.com. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
On Thu, 4 Sep 2008, ONKELINX, Thierry wrote:> Dear Luc, > > You should send this kind of questions to the general mailing list > (r-help at r-project.org) instead of the developer list. > > To answer your question: have a look at ?by and ?aggregateAnd ?ave .> HTH, > > Thierry> -----Oorspronkelijk bericht----- > Van: r-devel-bounces at r-project.org > [mailto:r-devel-bounces at r-project.org] Namens RFTW > Verzonden: donderdag 4 september 2008 9:07 > Aan: r-devel at r-project.org > Onderwerp: [Rd] calculating means per group > > > Hi all > I have a very basic question, yet i have not found how to do it. > > Suppose my dataset looks like this: > > Year Area value > 1 a 20 > 1 a 25 > 1 a 28 > 1 a 31 > 1 a 23 > 1 b 25 > 1 b 28 > 1 b 23 > 1 b 19 > 2 a 25 > 2 a 23 > 2 a 24 > 2 a 26 > 2 b 27 > 2 b 28 > 2 b 20 > 2 b 25 > 2 b 28 > > > Now, i want to calculate a MEAN per year per area. How do i do that? > > With mean(value) i calculate the mean of all values of course. I just > need > to know how to group year and area correctly. > > I assume that i can use this grouping in other calculations too, right? > > > Cheers, > > Luc > -- > View this message in context: > http://www.nabble.com/calculating-means-per-group-tp19271479p19271479.ht > ml > Sent from the R devel mailing list archive at Nabble.com. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.%CRLF%The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document%CRLF% > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595