将每小时数据汇总到 R 列表中的每日数据
Summarize hourly data to daily data in a list in R
我正在尝试将每小时测量数据汇总为列表中每个元素的每日数据。
列表如下所示:
SE104:List of 3
..$ d20:List of 11
.. ..$ 2009:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2009-01-01" "2009-01-01" "2009-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2010:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2010-01-01" "2010-01-01" "2010-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2011:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2011-01-01" "2011-01-01" "2011-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2012:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2012-01-01" "2012-01-01" "2012-01-01" ...
.. .. ..$ SWC : num [1:8784] 43.1 43 42.8 42.7 42.7 ...
.. ..$ 2013:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2013-01-01" "2013-01-01" "2013-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2014:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2014-01-01" "2014-01-01" "2014-01-01" ...
.. .. ..$ SWC : num [1:8760] 37.2 37.2 37.3 37.2 37.1 ...
.. ..$ 2015:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2015-01-01" "2015-01-01" "2015-01-01" ...
.. .. ..$ SWC : num [1:8760] 37.3 37.3 37.3 37.3 37.3 ...
.. ..$ 2016:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2016-01-01" "2016-01-01" "2016-01-01" ...
.. .. ..$ SWC : num [1:8784] 36 36 36 36 35.9 ...
.. ..$ 2017:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2017-01-01" "2017-01-01" "2017-01-01" ...
.. .. ..$ SWC : num [1:8760] 32.9 32.9 32.9 32.9 32.9 ...
.. ..$ 2018:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2018-01-01" "2018-01-01" "2018-01-01" ...
.. .. ..$ SWC : num [1:8760] 35 35.1 35.2 35.2 35.2 ...
.. ..$ 2019:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2019-01-01" "2019-01-01" "2019-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN ...
..$ d50:List of 11
.. ..$ 2009:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2009-01-01" "2009-01-01" "2009-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2010:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2010-01-01" "2010-01-01" "2010-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2011:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2011-01-01" "2011-01-01" "2011-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2012:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2012-01-01" "2012-01-01" "2012-01-01" ...
.. .. ..$ SWC : num [1:8784] 39.8 39.6 39.4 39.3 39.3 ...
.. ..$ 2013:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2013-01-01" "2013-01-01" "2013-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2014:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2014-01-01" "2014-01-01" "2014-01-01" ...
.. .. ..$ SWC : num [1:8760] 32.7 32.7 32.8 32.7 32.7 ...
.. ..$ 2015:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2015-01-01" "2015-01-01" "2015-01-01" ...
.. .. ..$ SWC : num [1:8760] 33.2 33.2 33.2 33.2 33.2 ...
.. ..$ 2016:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2016-01-01" "2016-01-01" "2016-01-01" ...
.. .. ..$ SWC : num [1:8784] 31.9 31.9 31.9 31.9 31.9 ...
.. ..$ 2017:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2017-01-01" "2017-01-01" "2017-01-01" ...
.. .. ..$ SWC : num [1:8760] 27.9 27.9 27.9 27.9 27.9 ...
.. ..$ 2018:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2018-01-01" "2018-01-01" "2018-01-01" ...
.. .. ..$ SWC : num [1:8760] 29.2 29.2 29.2 29.2 29.2 ...
.. ..$ 2019:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2019-01-01" "2019-01-01" "2019-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN ...
..$ d5 :List of 11
.. ..$ 2009:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2009-01-01" "2009-01-01" "2009-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2010:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2010-01-01" "2010-01-01" "2010-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2011:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2011-01-01" "2011-01-01" "2011-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2012:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2012-01-01" "2012-01-01" "2012-01-01" ...
.. .. ..$ SWC : num [1:8784] 58.4 58.4 58.3 58.2 58.2 ...
.. ..$ 2013:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2013-01-01" "2013-01-01" "2013-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2014:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2014-01-01" "2014-01-01" "2014-01-01" ...
.. .. ..$ SWC : num [1:8760] 47.4 47.4 47.4 47.4 47.4 ...
.. ..$ 2015:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2015-01-01" "2015-01-01" "2015-01-01" ...
.. .. ..$ SWC : num [1:8760] 49 49.1 49 49.1 49.1 ...
.. ..$ 2016:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2016-01-01" "2016-01-01" "2016-01-01" ...
.. .. ..$ SWC : num [1:8784] 43.7 43.7 43.7 43.8 43.7 ...
.. ..$ 2017:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2017-01-01" "2017-01-01" "2017-01-01" ...
.. .. ..$ SWC : num [1:8760] 39.1 39.2 39.1 39.2 39.2 ...
.. ..$ 2018:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2018-01-01" "2018-01-01" "2018-01-01" ...
.. .. ..$ SWC : num [1:8760] 45.8 46 46 45.9 45.7 ...
.. ..$ 2019:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2019-01-01" "2019-01-01" "2019-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN ...
$ SE105:List of 3
..$ d20:List of 11
.. ..$ 2009:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2009-01-01" "2009-01-01" "2009-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2010:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2010-01-01" "2010-01-01" "2010-01-01" ...
.. .. ..$ SWC : num [1:8760] 45.4 45.2 45 44.9 44.8 ...
.. ..$ 2011:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2011-01-01" "2011-01-01" "2011-01-01" ...
.. .. ..$ SWC : num [1:8760] 39.6 39.6 39.6 39.6 39.6 ...
.. ..$ 2012:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format:
所以你可以看到我的列表有多个级别。第一级是一个包含 150 个列表的大列表。这 150 个列表中的每个列表都包含 3 个列表(d20、d50、d5),其中包含 2009-2019 年每年的 11 个数据框。
存储在列表中的每个数据帧如下所示:
structure(list(Date = structure(c(14245, 14245, 14245, 14245,
14245, 14245, 14245, 14245, 14245, 14245, 14245, 14245, 14245,
14245, 14245, 14245, 14245, 14245, 14245, 14245, 14245, 14245,
14245, 14245, 14246, 14246, 14246, 14246, 14246, 14246), class = "Date"),
SWC = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN)), row.names = c(NA,
30L), class = "data.frame")
我想总结数据,以便每天获得一个值,我可以只对一个子集化的数据帧执行此操作,但我不能对其中的每个元素执行此操作list. 我想我必须使用 lapply() 但我不知道该怎么做。它应该是这样的:
Date mean_SWC
1 2009-01-01 NaN
2 2009-01-02 NaN
非常感谢您的帮助!用户已经在另一个问题中帮助我解决了 lapply() 函数,但看起来我不明白如何使用它,因为我不能在这里做。因此,如果有人可以提供帮助并且确实使用了 lapply() ,如果有人可以向我解释一下,我将不胜感激。
由于没有可用的样本数据,定义
A = data.frame(Date = c("31.01.2020", "31.01.2020", "31.01.2020"), SWC = c(25, 100, 2))
B = data.frame(Date = c("31.01.2020", "31.01.2020", "31.01.2020"), SWC = c(25, 100, 2))
C = data.frame(Date = c("31.01.2020", "31.01.2020", "31.01.2020"), SWC = c(25, 100, 2))
D = data.frame(Date = c("31.01.2020", "31.01.2020", "31.01.2020"), SWC = c(25, 100, 2))
E = list(A, B)
F2 = list(C, D)
G = list(E, F2)
H = list(G, G)
这应该模仿列表的嵌套结构。
那么一种可能就是多次使用lapply和聚合
lapply(H, function(x)
lapply(x, function(y)
lapply(y, function(z)
aggregate(SWC~Date, data = z, FUN = function(a) c(mean = mean(a), count = length(a))))))
产生结果
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[1]][[1]][[2]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[1]][[2]]
[[1]][[2]][[1]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[1]][[2]][[2]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[2]][[1]][[2]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[2]][[2]]
[[2]][[2]][[1]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[2]][[2]][[2]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
这是你的想法吗?
您有一个嵌套的列表(传感器)列表(测量值)列表(数据框),其中每个列表都需要一个 lapply
。
我确定还有一个 rapply
解决方案,虽然可能。
既然我已经回答了你的一个 我假设你想通过每个传感器进行聚合。
您可以通过折叠一些子列表来改进 lapply
方法。因此,您可以考虑例如这三个解决方案。
解决方案 1
生成与原始列表结构相似的列表结构,每年只需 aggregate
d 天。
res1 <- lapply(dat, function(se)
lapply(se, function(m)
lapply(m, function(d)
aggregate(swc ~ date, d, sum))))
结果列表的结构
str(res1, 3)
# List of 3
# $ SE104:List of 3
# ..$ d20:List of 11
# .. ..$ 2009:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2010:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2011:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2012:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2013:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2014:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2015:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2016:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2017:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2018:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2019:'data.frame': 365 obs. of 2 variables:
# ..$ d50:List of 11
# .. ..$ 2009:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2010:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2011:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2012:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2013:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2014:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2015:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2016:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2017:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2018:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2019:'data.frame': 365 obs. of 2 variables:
# ..$ d5 :List of 11
# .. ..$ 2009:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2010:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2011:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2012:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2013:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2014:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2015:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2016:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2017:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2018:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2019:'data.frame': 365 obs. of 2 variables:
# $ SE105:List of 3
# ..$ d20:List of 11
# .. ..$ 2009:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2010:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2011:'data.frame': 365 obs. of 2 variables:
# [...]
结果的示例数据框
head(res1$SE104$d20$`2009`)
# date swc
# 1 2009-01-01 7.642943
# 2 2009-01-02 2.251837
# 3 2009-01-03 -2.579394
# 4 2009-01-04 5.265464
# 5 2009-01-05 -2.891591
# 6 2009-01-06 -12.054497
解决方案 1(增强)
解决方案 1 的增强版,当 ncol(x) == 0
使用 tryCatch()
.
时绕过 aggregate
的错误
dat2 <- dat ## copy of dat
## intentionally make one data frame of nrow == 0
dat2$SE104$d20$`2009` <- data.frame(date=as.Date(character()),
swc=double())
res1.enh <- lapply(dat2, function(se)
lapply(se, function(m)
setNames(lapply(1:length(m), function(d)
tryCatch(aggregate(swc ~ date, m[[d]], function(x) sum(x, na.rm=TRUE)),
error=function(e) {
## mimicks aggregate result with NA column for swc
y <- as.numeric(names(m)[d])
cbind.data.frame(
date=as.Date(as.character(seq(ISOdate(y, 1, 1, 0),
ISOdate(y, 12, 31, 0), by="day"))),
swc=NA)
})), names(m))))
ncol == 0 时的示例结果
head(res1.enh$SE104$d20$`2009`)
# date swc
# 1 2009-01-01 NA
# 2 2009-01-02 NA
# 3 2009-01-03 NA
# 4 2009-01-04 NA
# 5 2009-01-05 NA
# 6 2009-01-06 NA
**正常结果*
head(res1.enh$SE104$d20$`2010`)
# date swc
# 1 2010-01-01 -1.001692
# 2 2010-01-02 -2.531776
# 3 2010-01-03 6.036390
# 4 2010-01-04 -5.470039
# 5 2010-01-05 -1.659984
# 6 2010-01-06 -8.495954
解决方案 2
与解决方案 1 相似,年份已折叠。
res2 <- lapply(dat, function(se)
lapply(se, function(m)
aggregate(swc ~ date, do.call(rbind, m), sum)))
结果列表的结构
str(res2)
# List of 3
# $ SE104:List of 3
# ..$ d20:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 7.64 2.25 -2.58 5.27 -2.89 ...
# ..$ d50:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] -7.658 -10.792 2.617 0.811 4.529 ...
# ..$ d5 :'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 2.52 2.01 7.26 5.33 -5.01 ...
# $ SE105:List of 3
# ..$ d20:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 5.88 -5.2 -7.11 2.57 2.29 ...
# ..$ d50:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] -10.09 -6.48 4.57 -7.04 -7.81 ...
# ..$ d5 :'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] -0.329 -4.42 -1.826 -7.249 1.083 ...
# $ SE106:List of 3
# ..$ d20:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 4.905 -4.207 2.128 0.121 -2.668 ...
# ..$ d50:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 4.092 -4.983 -7.234 -6.362 -0.135 ...
# ..$ d5 :'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 2.13 4.26 10.59 0.56 2.36 ...
结果的示例数据框
head(res2$SE104$d20)
# date swc
# 1 2009-01-01 7.642943
# 2 2009-01-02 2.251837
# 3 2009-01-03 -2.579394
# 4 2009-01-04 5.265464
# 5 2009-01-05 -2.891591
# 6 2009-01-06 -12.054497
解决方案 3
一种宽幅解2.
res3 <- lapply(dat, function(se) {
tmp <- lapply(se, function(m) do.call(data.frame, aggregate(swc ~ date, do.call(rbind, m), sum)))
data.frame(date=el(tmp)[["date"]], setNames(mapply(`[`, tmp, 2), names(se)))
})
结果列表的结构
str(res3)
# List of 3
# $ SE104:'data.frame': 4017 obs. of 4 variables:
# ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# ..$ d20 : num [1:4017] 7.64 2.25 -2.58 5.27 -2.89 ...
# ..$ d50 : num [1:4017] -7.658 -10.792 2.617 0.811 4.529 ...
# ..$ d5 : num [1:4017] 2.52 2.01 7.26 5.33 -5.01 ...
# $ SE105:'data.frame': 4017 obs. of 4 variables:
# ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# ..$ d20 : num [1:4017] 5.88 -5.2 -7.11 2.57 2.29 ...
# ..$ d50 : num [1:4017] -10.09 -6.48 4.57 -7.04 -7.81 ...
# ..$ d5 : num [1:4017] -0.329 -4.42 -1.826 -7.249 1.083 ...
# $ SE106:'data.frame': 4017 obs. of 4 variables:
# ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# ..$ d20 : num [1:4017] 4.905 -4.207 2.128 0.121 -2.668 ...
# ..$ d50 : num [1:4017] 4.092 -4.983 -7.234 -6.362 -0.135 ...
# ..$ d5 : num [1:4017] 2.13 4.26 10.59 0.56 2.36 ...
结果列表中三个数据帧的头部
lapply(res3, head)
# $SE104
# date d20 d50 d5
# 1 2009-01-01 7.642943 -7.6576879 2.520563
# 2 2009-01-02 2.251837 -10.7916635 2.012231
# 3 2009-01-03 -2.579394 2.6173333 7.260413
# 4 2009-01-04 5.265464 0.8107366 5.327772
# 5 2009-01-05 -2.891591 4.5289522 -5.014450
# 6 2009-01-06 -12.054497 -5.9274899 1.441439
#
# $SE105
# date d20 d50 d5
# 1 2009-01-01 5.880239 -10.093960 -0.3288049
# 2 2009-01-02 -5.196587 -6.478163 -4.4196227
# 3 2009-01-03 -7.114667 4.568871 -1.8259147
# 4 2009-01-04 2.565384 -7.039717 -7.2492291
# 5 2009-01-05 2.289701 -7.808063 1.0832635
# 6 2009-01-06 -1.674715 4.360558 2.2994494
#
# $SE106
# date d20 d50 d5
# 1 2009-01-01 4.9053760 4.0917553 2.132417
# 2 2009-01-02 -4.2074579 -4.9825570 4.256667
# 3 2009-01-03 2.1284944 -7.2339473 10.588332
# 4 2009-01-04 0.1208879 -6.3617098 0.560269
# 5 2009-01-05 -2.6676826 -0.1350602 2.364006
# 6 2009-01-06 -3.0595029 7.9359285 6.903543
玩具数据:
dat <- setNames(replicate(3, setNames(replicate(3, setNames(lapply(2009:2019, function(y) {
d <- expand.grid(date=as.Date(as.character(seq(ISOdate(y, 1, 1, 0), ISOdate(y, 12, 31, 0), by="day"))),
hour=1:24)
d$swc <- rnorm(nrow(d))
d[order(d$date), -2]
}), 2009:2019), simplify=F), c("d20", "d50", "d5")), simplify=F), c("SE104", "SE105", "SE106"))
我正在尝试将每小时测量数据汇总为列表中每个元素的每日数据。
列表如下所示:
SE104:List of 3
..$ d20:List of 11
.. ..$ 2009:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2009-01-01" "2009-01-01" "2009-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2010:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2010-01-01" "2010-01-01" "2010-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2011:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2011-01-01" "2011-01-01" "2011-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2012:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2012-01-01" "2012-01-01" "2012-01-01" ...
.. .. ..$ SWC : num [1:8784] 43.1 43 42.8 42.7 42.7 ...
.. ..$ 2013:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2013-01-01" "2013-01-01" "2013-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2014:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2014-01-01" "2014-01-01" "2014-01-01" ...
.. .. ..$ SWC : num [1:8760] 37.2 37.2 37.3 37.2 37.1 ...
.. ..$ 2015:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2015-01-01" "2015-01-01" "2015-01-01" ...
.. .. ..$ SWC : num [1:8760] 37.3 37.3 37.3 37.3 37.3 ...
.. ..$ 2016:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2016-01-01" "2016-01-01" "2016-01-01" ...
.. .. ..$ SWC : num [1:8784] 36 36 36 36 35.9 ...
.. ..$ 2017:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2017-01-01" "2017-01-01" "2017-01-01" ...
.. .. ..$ SWC : num [1:8760] 32.9 32.9 32.9 32.9 32.9 ...
.. ..$ 2018:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2018-01-01" "2018-01-01" "2018-01-01" ...
.. .. ..$ SWC : num [1:8760] 35 35.1 35.2 35.2 35.2 ...
.. ..$ 2019:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2019-01-01" "2019-01-01" "2019-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN ...
..$ d50:List of 11
.. ..$ 2009:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2009-01-01" "2009-01-01" "2009-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2010:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2010-01-01" "2010-01-01" "2010-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2011:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2011-01-01" "2011-01-01" "2011-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2012:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2012-01-01" "2012-01-01" "2012-01-01" ...
.. .. ..$ SWC : num [1:8784] 39.8 39.6 39.4 39.3 39.3 ...
.. ..$ 2013:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2013-01-01" "2013-01-01" "2013-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2014:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2014-01-01" "2014-01-01" "2014-01-01" ...
.. .. ..$ SWC : num [1:8760] 32.7 32.7 32.8 32.7 32.7 ...
.. ..$ 2015:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2015-01-01" "2015-01-01" "2015-01-01" ...
.. .. ..$ SWC : num [1:8760] 33.2 33.2 33.2 33.2 33.2 ...
.. ..$ 2016:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2016-01-01" "2016-01-01" "2016-01-01" ...
.. .. ..$ SWC : num [1:8784] 31.9 31.9 31.9 31.9 31.9 ...
.. ..$ 2017:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2017-01-01" "2017-01-01" "2017-01-01" ...
.. .. ..$ SWC : num [1:8760] 27.9 27.9 27.9 27.9 27.9 ...
.. ..$ 2018:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2018-01-01" "2018-01-01" "2018-01-01" ...
.. .. ..$ SWC : num [1:8760] 29.2 29.2 29.2 29.2 29.2 ...
.. ..$ 2019:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2019-01-01" "2019-01-01" "2019-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN ...
..$ d5 :List of 11
.. ..$ 2009:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2009-01-01" "2009-01-01" "2009-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2010:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2010-01-01" "2010-01-01" "2010-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2011:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2011-01-01" "2011-01-01" "2011-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2012:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2012-01-01" "2012-01-01" "2012-01-01" ...
.. .. ..$ SWC : num [1:8784] 58.4 58.4 58.3 58.2 58.2 ...
.. ..$ 2013:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2013-01-01" "2013-01-01" "2013-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2014:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2014-01-01" "2014-01-01" "2014-01-01" ...
.. .. ..$ SWC : num [1:8760] 47.4 47.4 47.4 47.4 47.4 ...
.. ..$ 2015:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2015-01-01" "2015-01-01" "2015-01-01" ...
.. .. ..$ SWC : num [1:8760] 49 49.1 49 49.1 49.1 ...
.. ..$ 2016:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format: "2016-01-01" "2016-01-01" "2016-01-01" ...
.. .. ..$ SWC : num [1:8784] 43.7 43.7 43.7 43.8 43.7 ...
.. ..$ 2017:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2017-01-01" "2017-01-01" "2017-01-01" ...
.. .. ..$ SWC : num [1:8760] 39.1 39.2 39.1 39.2 39.2 ...
.. ..$ 2018:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2018-01-01" "2018-01-01" "2018-01-01" ...
.. .. ..$ SWC : num [1:8760] 45.8 46 46 45.9 45.7 ...
.. ..$ 2019:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2019-01-01" "2019-01-01" "2019-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN ...
$ SE105:List of 3
..$ d20:List of 11
.. ..$ 2009:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2009-01-01" "2009-01-01" "2009-01-01" ...
.. .. ..$ SWC : num [1:8760] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
.. ..$ 2010:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2010-01-01" "2010-01-01" "2010-01-01" ...
.. .. ..$ SWC : num [1:8760] 45.4 45.2 45 44.9 44.8 ...
.. ..$ 2011:'data.frame': 8760 obs. of 2 variables:
.. .. ..$ Date: Date[1:8760], format: "2011-01-01" "2011-01-01" "2011-01-01" ...
.. .. ..$ SWC : num [1:8760] 39.6 39.6 39.6 39.6 39.6 ...
.. ..$ 2012:'data.frame': 8784 obs. of 2 variables:
.. .. ..$ Date: Date[1:8784], format:
所以你可以看到我的列表有多个级别。第一级是一个包含 150 个列表的大列表。这 150 个列表中的每个列表都包含 3 个列表(d20、d50、d5),其中包含 2009-2019 年每年的 11 个数据框。
存储在列表中的每个数据帧如下所示:
structure(list(Date = structure(c(14245, 14245, 14245, 14245,
14245, 14245, 14245, 14245, 14245, 14245, 14245, 14245, 14245,
14245, 14245, 14245, 14245, 14245, 14245, 14245, 14245, 14245,
14245, 14245, 14246, 14246, 14246, 14246, 14246, 14246), class = "Date"),
SWC = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN)), row.names = c(NA,
30L), class = "data.frame")
我想总结数据,以便每天获得一个值,我可以只对一个子集化的数据帧执行此操作,但我不能对其中的每个元素执行此操作list. 我想我必须使用 lapply() 但我不知道该怎么做。它应该是这样的:
Date mean_SWC
1 2009-01-01 NaN
2 2009-01-02 NaN
非常感谢您的帮助!用户已经在另一个问题中帮助我解决了 lapply() 函数,但看起来我不明白如何使用它,因为我不能在这里做。因此,如果有人可以提供帮助并且确实使用了 lapply() ,如果有人可以向我解释一下,我将不胜感激。
由于没有可用的样本数据,定义
A = data.frame(Date = c("31.01.2020", "31.01.2020", "31.01.2020"), SWC = c(25, 100, 2))
B = data.frame(Date = c("31.01.2020", "31.01.2020", "31.01.2020"), SWC = c(25, 100, 2))
C = data.frame(Date = c("31.01.2020", "31.01.2020", "31.01.2020"), SWC = c(25, 100, 2))
D = data.frame(Date = c("31.01.2020", "31.01.2020", "31.01.2020"), SWC = c(25, 100, 2))
E = list(A, B)
F2 = list(C, D)
G = list(E, F2)
H = list(G, G)
这应该模仿列表的嵌套结构。
那么一种可能就是多次使用lapply和聚合
lapply(H, function(x)
lapply(x, function(y)
lapply(y, function(z)
aggregate(SWC~Date, data = z, FUN = function(a) c(mean = mean(a), count = length(a))))))
产生结果
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[1]][[1]][[2]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[1]][[2]]
[[1]][[2]][[1]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[1]][[2]][[2]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[2]][[1]][[2]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[2]][[2]]
[[2]][[2]][[1]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
[[2]][[2]][[2]]
Date SWC.mean SWC.count
1 31.01.2020 42.33333 3.00000
这是你的想法吗?
您有一个嵌套的列表(传感器)列表(测量值)列表(数据框),其中每个列表都需要一个 lapply
。
我确定还有一个 rapply
解决方案,虽然可能。
既然我已经回答了你的一个
您可以通过折叠一些子列表来改进 lapply
方法。因此,您可以考虑例如这三个解决方案。
解决方案 1
生成与原始列表结构相似的列表结构,每年只需 aggregate
d 天。
res1 <- lapply(dat, function(se)
lapply(se, function(m)
lapply(m, function(d)
aggregate(swc ~ date, d, sum))))
结果列表的结构
str(res1, 3)
# List of 3
# $ SE104:List of 3
# ..$ d20:List of 11
# .. ..$ 2009:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2010:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2011:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2012:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2013:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2014:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2015:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2016:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2017:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2018:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2019:'data.frame': 365 obs. of 2 variables:
# ..$ d50:List of 11
# .. ..$ 2009:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2010:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2011:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2012:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2013:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2014:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2015:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2016:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2017:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2018:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2019:'data.frame': 365 obs. of 2 variables:
# ..$ d5 :List of 11
# .. ..$ 2009:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2010:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2011:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2012:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2013:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2014:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2015:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2016:'data.frame': 366 obs. of 2 variables:
# .. ..$ 2017:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2018:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2019:'data.frame': 365 obs. of 2 variables:
# $ SE105:List of 3
# ..$ d20:List of 11
# .. ..$ 2009:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2010:'data.frame': 365 obs. of 2 variables:
# .. ..$ 2011:'data.frame': 365 obs. of 2 variables:
# [...]
结果的示例数据框
head(res1$SE104$d20$`2009`)
# date swc
# 1 2009-01-01 7.642943
# 2 2009-01-02 2.251837
# 3 2009-01-03 -2.579394
# 4 2009-01-04 5.265464
# 5 2009-01-05 -2.891591
# 6 2009-01-06 -12.054497
解决方案 1(增强)
解决方案 1 的增强版,当 ncol(x) == 0
使用 tryCatch()
.
aggregate
的错误
dat2 <- dat ## copy of dat
## intentionally make one data frame of nrow == 0
dat2$SE104$d20$`2009` <- data.frame(date=as.Date(character()),
swc=double())
res1.enh <- lapply(dat2, function(se)
lapply(se, function(m)
setNames(lapply(1:length(m), function(d)
tryCatch(aggregate(swc ~ date, m[[d]], function(x) sum(x, na.rm=TRUE)),
error=function(e) {
## mimicks aggregate result with NA column for swc
y <- as.numeric(names(m)[d])
cbind.data.frame(
date=as.Date(as.character(seq(ISOdate(y, 1, 1, 0),
ISOdate(y, 12, 31, 0), by="day"))),
swc=NA)
})), names(m))))
ncol == 0 时的示例结果
head(res1.enh$SE104$d20$`2009`)
# date swc
# 1 2009-01-01 NA
# 2 2009-01-02 NA
# 3 2009-01-03 NA
# 4 2009-01-04 NA
# 5 2009-01-05 NA
# 6 2009-01-06 NA
**正常结果*
head(res1.enh$SE104$d20$`2010`)
# date swc
# 1 2010-01-01 -1.001692
# 2 2010-01-02 -2.531776
# 3 2010-01-03 6.036390
# 4 2010-01-04 -5.470039
# 5 2010-01-05 -1.659984
# 6 2010-01-06 -8.495954
解决方案 2
与解决方案 1 相似,年份已折叠。
res2 <- lapply(dat, function(se)
lapply(se, function(m)
aggregate(swc ~ date, do.call(rbind, m), sum)))
结果列表的结构
str(res2)
# List of 3
# $ SE104:List of 3
# ..$ d20:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 7.64 2.25 -2.58 5.27 -2.89 ...
# ..$ d50:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] -7.658 -10.792 2.617 0.811 4.529 ...
# ..$ d5 :'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 2.52 2.01 7.26 5.33 -5.01 ...
# $ SE105:List of 3
# ..$ d20:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 5.88 -5.2 -7.11 2.57 2.29 ...
# ..$ d50:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] -10.09 -6.48 4.57 -7.04 -7.81 ...
# ..$ d5 :'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] -0.329 -4.42 -1.826 -7.249 1.083 ...
# $ SE106:List of 3
# ..$ d20:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 4.905 -4.207 2.128 0.121 -2.668 ...
# ..$ d50:'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 4.092 -4.983 -7.234 -6.362 -0.135 ...
# ..$ d5 :'data.frame': 4017 obs. of 2 variables:
# .. ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# .. ..$ swc : num [1:4017] 2.13 4.26 10.59 0.56 2.36 ...
结果的示例数据框
head(res2$SE104$d20)
# date swc
# 1 2009-01-01 7.642943
# 2 2009-01-02 2.251837
# 3 2009-01-03 -2.579394
# 4 2009-01-04 5.265464
# 5 2009-01-05 -2.891591
# 6 2009-01-06 -12.054497
解决方案 3
一种宽幅解2.
res3 <- lapply(dat, function(se) {
tmp <- lapply(se, function(m) do.call(data.frame, aggregate(swc ~ date, do.call(rbind, m), sum)))
data.frame(date=el(tmp)[["date"]], setNames(mapply(`[`, tmp, 2), names(se)))
})
结果列表的结构
str(res3)
# List of 3
# $ SE104:'data.frame': 4017 obs. of 4 variables:
# ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# ..$ d20 : num [1:4017] 7.64 2.25 -2.58 5.27 -2.89 ...
# ..$ d50 : num [1:4017] -7.658 -10.792 2.617 0.811 4.529 ...
# ..$ d5 : num [1:4017] 2.52 2.01 7.26 5.33 -5.01 ...
# $ SE105:'data.frame': 4017 obs. of 4 variables:
# ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# ..$ d20 : num [1:4017] 5.88 -5.2 -7.11 2.57 2.29 ...
# ..$ d50 : num [1:4017] -10.09 -6.48 4.57 -7.04 -7.81 ...
# ..$ d5 : num [1:4017] -0.329 -4.42 -1.826 -7.249 1.083 ...
# $ SE106:'data.frame': 4017 obs. of 4 variables:
# ..$ date: Date[1:4017], format: "2009-01-01" "2009-01-02" "2009-01-03" ...
# ..$ d20 : num [1:4017] 4.905 -4.207 2.128 0.121 -2.668 ...
# ..$ d50 : num [1:4017] 4.092 -4.983 -7.234 -6.362 -0.135 ...
# ..$ d5 : num [1:4017] 2.13 4.26 10.59 0.56 2.36 ...
结果列表中三个数据帧的头部
lapply(res3, head)
# $SE104
# date d20 d50 d5
# 1 2009-01-01 7.642943 -7.6576879 2.520563
# 2 2009-01-02 2.251837 -10.7916635 2.012231
# 3 2009-01-03 -2.579394 2.6173333 7.260413
# 4 2009-01-04 5.265464 0.8107366 5.327772
# 5 2009-01-05 -2.891591 4.5289522 -5.014450
# 6 2009-01-06 -12.054497 -5.9274899 1.441439
#
# $SE105
# date d20 d50 d5
# 1 2009-01-01 5.880239 -10.093960 -0.3288049
# 2 2009-01-02 -5.196587 -6.478163 -4.4196227
# 3 2009-01-03 -7.114667 4.568871 -1.8259147
# 4 2009-01-04 2.565384 -7.039717 -7.2492291
# 5 2009-01-05 2.289701 -7.808063 1.0832635
# 6 2009-01-06 -1.674715 4.360558 2.2994494
#
# $SE106
# date d20 d50 d5
# 1 2009-01-01 4.9053760 4.0917553 2.132417
# 2 2009-01-02 -4.2074579 -4.9825570 4.256667
# 3 2009-01-03 2.1284944 -7.2339473 10.588332
# 4 2009-01-04 0.1208879 -6.3617098 0.560269
# 5 2009-01-05 -2.6676826 -0.1350602 2.364006
# 6 2009-01-06 -3.0595029 7.9359285 6.903543
玩具数据:
dat <- setNames(replicate(3, setNames(replicate(3, setNames(lapply(2009:2019, function(y) {
d <- expand.grid(date=as.Date(as.character(seq(ISOdate(y, 1, 1, 0), ISOdate(y, 12, 31, 0), by="day"))),
hour=1:24)
d$swc <- rnorm(nrow(d))
d[order(d$date), -2]
}), 2009:2019), simplify=F), c("d20", "d50", "d5")), simplify=F), c("SE104", "SE105", "SE106"))