Hüsamettin Tayşi
Hüsamettin Tayşi

Reputation: 574

How to convert hourly data to 6/12/24 hourly data

I am new to here and also R.

For example I have this kind of rainfall data. As is seen in the data for a day, I have hourly rainfall data and I want to convert this hourly data to 6-hourly, 12-hourly and daily datas like the sample. How can I achieve this in R?

sample data

Upvotes: 1

Views: 198

Answers (2)

Frank
Frank

Reputation: 66819

There are many ways to aggregate in R. See Grouping functions (tapply, by, aggregate) and the *apply family

Here's another way with tapply:

# input data (since OP contains none)
> v = replace(numeric(24), c(3:4, 6:10), c(0.6, 0.2, 0.2, 3.2, 0.8, 0.4, 1))
> 
> lapply(c(1, 6, 12, 24), function(n) tapply(v, rep(1:24, each=n, length.out=24), sum))
[[1]]
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 
0.0 0.0 0.6 0.2 0.0 0.2 3.2 0.8 0.4 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 

[[2]]
  1   2   3   4 
1.0 5.4 0.0 0.0 

[[3]]
  1   2 
6.4 0.0 

[[4]]
  1 
6.4 

Upvotes: 2

Hüsamettin Tayşi
Hüsamettin Tayşi

Reputation: 574

I solved it with codes below.

    y<-as.matrix(sapply(x, as.numeric)) 
h<-sapply(c(6, 12, 24), function(hrs) colSums(matrix(y, ncol=length(x)/hrs)))

Upvotes: 2

Related Questions