[R] Aggregating dataset to means/day
Ivan Calandra
ivan.calandra at uni-hamburg.de
Wed Mar 16 14:17:27 CET 2011
Hi,
Look at ?aggregate.
df <- read.table(textConnection("Year Day Time herring.density
2007 47 10.36 2.2
2007 47 11.50 1.1
2007 47 14.24 1.4
2007 66 9.35 2.5"), header=TRUE)
You could do this:
aggregate(herring.density~Year+Day, data=df, FUN=mean)
And for the second question:
aggregate(herring.density~Year+Day, data=df[df$Time>=10 & df$Time<=16,],
FUN=mean)
Alternatively, you can use the function summaryBy in the doBy package.
You use it exactly the same way as aggregate. You'll only get different
column names.
HTH,
Ivan
Le 3/16/2011 11:17, OA Fatnes a écrit :
> Hi,
>
> I have a dataset with many observations some days while only one others. I
> would like to calculate a mean value per day and then do regression analysis
> on the means.
>
> This is what I have:
>
> Year Day Time herring.density
> 2007 47 10.36 2.2
> 2007 47 11.50 1.1
> 2007 47 14.24 1.4
> 2007 66 9.35 2.5
>
> This is what I want:
>
> Year Day herring.density
> 2007 47 1.57
> 2007 66 2.25
>
> I would also might like to extract means between time 10-16 (h) so that
>
> Year Day herring.density
> 2007 47 1.25
>
> Any idea on how to do this?
>
> I have tried
>
> means07<-tapply(herring.density,Day,mean)
>
> Then I get means for every day, but means07 won't fit when plotted against
> days, because it is now shorter than Day. I also get a lot of NA values
> where there are only 1 observation that day...
>
> Help would be much appreciated!
>
> Cheers,
>
> Ole Andreas
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
More information about the R-help
mailing list