# [R] Calculating a daily average

David Winsemius dwinsemius at comcast.net
Mon Jun 21 17:42:10 CEST 2010

```On Jun 21, 2010, at 10:27 AM, ecvetano at uwaterloo.ca wrote:

> I have a set of data with 12 readings for temperature per day, with
> 180 days. I want to find the average temperature of each day. I am
> able to do this one by one, but with that many days to calculate the
> average for, it will get very long. I'm sure there is a faster way
> to do this, I just don't know how. What i have so far is:
>
>
> av1 <-  subset(ER9r, Day == 98, select = c (Depth1j:Depth0.75j))
> av1 <- mean(av1 [av1>0])
> av2 <-  subset(ER9r, Day == 99, select = c (Depth1j:Depth0.75j))
> av2 <- mean(av2 [av2>0])
> av3 <-  subset(ER9r, Day == 100, select = c (Depth1j:Depth0.75j))
> av3 <- mean(av3 [av3>0])
> av4 <-  subset(ER9r, Day == 101, select = c (Depth1j:Depth0.75j))
> av4 <- mean(av4 [av4>0])
> av5 <-  subset(ER9r, Day == 102, select = c (Depth1j:Depth0.75j))
> av5 <- mean(av5 [av5>0])
>

?tapply

(Having some trouble figuring out what you want to do with the
multiple columns, Depth1j:Depth0.75j. I would not have thought that
the application of mean() would work on an object of type= dataframe.
Did it work for you when you tested it?)

> Depth<- c("SML")
> Day<- c(98, 99, 100, 101, 102)
> Average<- c(av1, av2, av3, av4, av5)
> chlsummaryER9 <- data.frame (Depth, Day, Average)
> chlsummaryER9
>
> Any ideas on how to do this in less steps?
>
> Thanks,
> Emilija
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help