如何将每小时数据转换为6/12/24每小时数据

时间:2019-03-13 11:06:48

标签: r aggregate

我是这里的新手,也是R。

例如,我有这种降雨数据。从一天的数据中可以看出,我有每小时的降雨数据,我想将此每小时的数据转换为6小时,12小时和每天的数据,例如样本。如何在R中实现这一目标?

sample data

2 个答案:

答案 0 :(得分:2)

我用下面的代码解决了它。

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

答案 1 :(得分:2)

有很多方法可以在R中进行聚合。请参见Grouping functions (tapply, by, aggregate) and the *apply family

这是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 
相关问题