0

I have hourly weather data of one year. I like to extract daily max, mean etc from it. so I was looking for help here. I came across following topic:

convert hourly rainfall data into daily in specific time interval

I could not able to understand the answer by Pop. As I have less than 50 points, I could not able to add comments there. So I am posting this question. Can anyone explain these commands?

fun <- function(s,i,j) { sum(s[i:(i+j-1)]) }
sapply(X=seq(1,24*nb_of_days,24),FUN=fun,s=your_time_serie,j=24)
Community
  • 1
  • 1
Barun
  • 185
  • 1
  • 2
  • 10
  • can you post `head` of your data? – Nishanth Mar 04 '14 at 08:00
  • 2
    It will be much easier to help if you provide a [**minimal, reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Henrik Mar 04 '14 at 08:01
  • please consider the data from the post i mentioned.........kindly explain the above function for that rainfall data – Barun Mar 04 '14 at 08:55
  • Those two lines are basically just sum the rainfall column. Why not do this problem by `levels` instead? The `max` rainfall of the first day is just the `max` rainfall according to the first `levels`. – Rich Scriven Mar 04 '14 at 09:51

1 Answers1

1

Suggest you use dplyr package for aggregation - see below:

rain<-read.table(sep="|",text="1970-01-05 00:00:00|1.0 
1970-01-05 01:00:00|1.0
1970-01-05 02:00:00|1.0
1970-01-05 03:00:00|1.0
1970-01-05 04:00:00|1.0
1970-01-05 05:00:00|3.6
1970-01-05 06:00:00|3.6
1970-01-05 07:00:00|2.2
1970-01-05 08:00:00|2.2
1970-01-05 09:00:00|2.2
1970-01-05 10:00:00|2.2
1970-01-05 11:00:00|2.2
1970-01-05 12:00:00|2.2
1970-01-05 13:00:00|2.2
1970-01-05 14:00:00|2.2
1970-01-05 15:00:00|2.2
1970-01-05 16:00:00|0.0
1970-01-05 17:00:00|0.0
1970-01-05 18:00:00|0.0
1970-01-05 19:00:00|0.0
1970-01-05 20:00:00|0.0
1970-01-05 21:00:00|0.0
1970-01-05 22:00:00|0.0
1970-01-05 23:00:00|0.0
1970-01-06 00:00:00|0.0")

require(dplyr) # for aggregation

group_by(rain,day=as.Date(rain$V1)) %.%
  summarise(total=sum(V2))

         day total
1 1970-01-05    32
2 1970-01-06     0
Troy
  • 8,581
  • 29
  • 32