将功能应用于具有多列的多列

时间:2019-04-18 12:50:51

标签: r

在下面的数据中,有20052006的值。每年有两列,分别为h1h24。函数myfun使用h1将每小时数据汇总为h6h12h24的6、12和24小时数据,并以{分别每年{1}},mean1mean6mean12并列出它们。

数据头:

mean24

我的整个数据是:

    X2005.h1  X2005.h24   X2006.h1 X2006.h24
1        0       0.0        0       0.0
2        0       0.0        0       8.6
3        0       3.4        0      11.2
4        0      21.4        0       8.4
5        0       1.8        0       0.0
6        0       1.4        0       0.0

功能:

myd<-structure(list(X2005.h1 = c(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, 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.6, 0.2, 0, 0, 0.6, 0.2, 0.6, 0.6, 0, 0, 0, 0.6, 1.2, 1.8, 
1.8, 1.2, 1, 1.2, 1.6, 1.2, 1.4, 1, 1.2, 0.8, 0.8, 0.2, 0, 0, 
1.6, 0.4, 0, 0.4, 1.2, 0.8, 0.2, 0.4, 0.2, 0.4, 0.4, 0.2, 0.2, 
0.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 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, 0.2, 
0.4, 0.4, 0.2, 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, 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, 
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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 2.2, 0, 0.4, 1, 0.6, 0, 
1.2, 1.6, 0.6, 0, 0.2, 0, 0, 0, 0, 0, 0.2, 0, 0, 0.2, 0, 0, 0.2, 
0, 0.2, 0, 0, 0, 0, 0.2, 0, 0, 0, 0, 0, 0, 0.2, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4, 0.2, 0, 1.2, 0.4, 1, 0.6, 
0.8, 1, 0, 0, 0, 0, 0), X2005.h24 = c(0, 0, 3.4, 21.4, 1.8, 1.4, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 8.6, 1.4, 0, 0, 0, 0, 0.6, 7, 6.4, 
21.8, 3.2, 0, 0, 0.6, 11.2, 0), X2006.h24 = c(0, 8.6, 11.2, 8.4, 
0, 0, 0, 0, 0, 7.8, 7.6, 1.2, 10.2, 7, 0, 0, 1.6, 7.6, 23.6, 
3.2, 0, 9, 31.4, 10, 2.2, 1.6, 0, 4.8, 10, 1.2, 4.6)), .Names = c("X2005.h1", 
"X2006.h1", "X2005.h24", "X2006.h24"))

我想要的是:

如前所述,通过汇总每小时数据myfun<- function(x) { y<-as.matrix(sapply(x, as.numeric)) #Aggregating hourly (h1) data to 6,12 hourly data h<-sapply(c(1, 6, 12), function(hrs) colSums(matrix(y, ncol=length(x)/hrs))) hours<-sapply(list(h[[1]], h[[2]], h[[3]]), function(qq) list(qq)) names(hours)<-c("h1", "h6", "h12") #Calculating means of 1, 6, 12 hourly data means<-sapply(list(hours[[1]], hours[[2]], hours[[3]]), function(ss) mean(ss, na.rm = T)) #means of 1, 6, 12 hourly data mean1<-means[[1]] mean6<-means[[2]] mean12<-means[[3]] mean24<- means[[4]] meanlist<-list(mean1, mean6, mean12, mean24) names(meanlist) <- c("mean1", "mean6", "mean12", "mean24") return(meanlist) } h1h6h12获得6-12-24个小时值。每年h24mean1mean6mean12都使用mean24函数进行循环计算。

意味着lapplymean1mean6很好。但是要获得mean12,我想使用mean24数据(x2005.h24,x2006.h24等),而不是聚合的h24数据。我该如何实现?

1 个答案:

答案 0 :(得分:0)

我不得不说我不习惯于分析列表中表示的数据,通常是data.frames或时间序列,这似乎效率不高,但是由于我没有更好的建议,因此这是一种方法可以完成,创建一个包含所需数据的新列表(每6和12小时一次),然后将平均值应用于每个元素

# list with 1h data
l1 <- myd[grepl('h1', names(myd))]

# list with 6h and 12h data
l6_12 <- names(myd)[grepl('h1', names(myd))] %>% 
  lapply(function(x) {
    setNames(lapply(c(6,12), 
                    function(h) rowSums(matrix(myd[[x]], ncol = h))), 
             c(gsub('h1', 'h6', x), gsub('h1', 'h12', x)))
  }) %>% 
  unlist(recursive = FALSE)

# list with 24h data
l24 <- myd[grepl('h24', names(myd))]

# combine all three together and then take the mean of each element (in this case 8 elements in total)
c(l1, l6_12, l24) %>% lapply(mean)