背景和数据结构
我将分享我的海量数据集的简化版本。这个简化版完全遵循了原始数据集的结构,但包含的列表元素、数据框、变量和观测值比原始数据集少。
根据这个问题得到的最受赞同的回答:如何创建一个优秀的 R 可重现示例?,我使用 dput(query1)
的输出来共享我的数据集,这样你可以通过在 R 控制台中复制/粘贴以下代码块立即在 R 中使用:
structure(list(plu = structure(list(year = structure(list(id = 1:3,
station = 100:102, pluMean = c(0.509068994778059, 1.92866478959912,
1.09517453602154), pluMax = c(0.0146962179957886, 0.802984389130343,
2.48170762478472)), .Names = c("id", "station", "pluMean",
"pluMax"), row.names = c(NA, -3L), class = "data.frame"), month = structure(list(
id = 1:3, station = 100:102, pluMean = c(0.66493845927034,
-1.3559338786041, 0.195600637750077), pluMax = c(0.503424623872161,
0.234402501255681, -0.440264545434053)), .Names = c("id",
"station", "pluMean", "pluMax"), row.names = c(NA, -3L), class = "data.frame"),
week = structure(list(id = 1:3, station = 100:102, pluMean = c(-0.608295829330578,
-1.10256919591373, 1.74984007126193), pluMax = c(0.969668266601551,
0.924426323739882, 3.47460867665884)), .Names = c("id", "station",
"pluMean", "pluMax"), row.names = c(NA, -3L), class = "data.frame")), .Names = c("year",
"month", "week")), tsa = structure(list(year = structure(list(
id = 1:3, station = 100:102, tsaMean = c(-1.49060721773042,
-0.684735418997484, 0.0586655881113975), tsaMax = c(0.25739838787582,
0.957634817758648, 1.37198023881125)), .Names = c("id", "station",
"tsaMean", "tsaMax"), row.names = c(NA, -3L), class = "data.frame"),
month = structure(list(id = 1:3, station = 100:102, tsaMean = c(-0.684668662999479,
-1.28087846387974, -0.600175481941456), tsaMax = c(0.962916941685075,
0.530773351897188, -0.217143593955998)), .Names = c("id",
"station", "tsaMean", "tsaMax"), row.names = c(NA, -3L), class = "data.frame"),
week = structure(list(id = 1:3, station = 100:102, tsaMean = c(0.376481732842365,
0.370435880636005, -0.105354927593471), tsaMax = c(1.93833635147645,
0.81176751708868, 0.744932493064975)), .Names = c("id", "station",
"tsaMean", "tsaMax"), row.names = c(NA, -3L), class = "data.frame")), .Names = c("year",
"month", "week"))), .Names = c("plu", "tsa"))
执行此操作后,如果执行
str(query1)
,您将获得我的示例数据集的结构: > str(query1)
List of 2
$ plu:List of 3
..$ year :'data.frame': 3 obs. of 4 variables:
.. ..$ id : int [1:3] 1 2 3
.. ..$ station: int [1:3] 100 101 102
.. ..$ pluMean: num [1:3] 0.509 1.929 1.095
.. ..$ pluMax : num [1:3] 0.0147 0.803 2.4817
..$ month:'data.frame': 3 obs. of 4 variables:
.. ..$ id : int [1:3] 1 2 3
.. ..$ station: int [1:3] 100 101 102
.. ..$ pluMean: num [1:3] 0.665 -1.356 0.196
.. ..$ pluMax : num [1:3] 0.503 0.234 -0.44
..$ week :'data.frame': 3 obs. of 4 variables:
.. ..$ id : int [1:3] 1 2 3
.. ..$ station: int [1:3] 100 101 102
.. ..$ pluMean: num [1:3] -0.608 -1.103 1.75
.. ..$ pluMax : num [1:3] 0.97 0.924 3.475
$ tsa:List of 3
..$ year :'data.frame': 3 obs. of 4 variables:
.. ..$ id : int [1:3] 1 2 3
.. ..$ station: int [1:3] 100 101 102
.. ..$ tsaMean: num [1:3] -1.4906 -0.6847 0.0587
.. ..$ tsaMax : num [1:3] 0.257 0.958 1.372
..$ month:'data.frame': 3 obs. of 4 variables:
.. ..$ id : int [1:3] 1 2 3
.. ..$ station: int [1:3] 100 101 102
.. ..$ tsaMean: num [1:3] -0.685 -1.281 -0.6
.. ..$ tsaMax : num [1:3] 0.963 0.531 -0.217
..$ week :'data.frame': 3 obs. of 4 variables:
.. ..$ id : int [1:3] 1 2 3
.. ..$ station: int [1:3] 100 101 102
.. ..$ tsaMean: num [1:3] 0.376 0.37 -0.105
.. ..$ tsaMax : num [1:3] 1.938 0.812 0.745
那么它是如何读取的?我有一个由2个参数元素(
plu
和tsa
)组成的大列表(query1
),每个这两个参数元素都是一个列表,由3个元素(year
、month
、week
)组成,每个这3个元素又是由相同的4个变量列(id
、station
、mean
、max
)和完全相同数量的观测值(3
)组成的timeInterval数据框。
我想要实现什么
我想要通过id
和station
的full_join
将所有具有相同名称(year
、month
、week
)的timeInterval数据框进行编程连接。full_join
结果将得到一份新列表(query1Changed
),其中包含3个数据框(year
、month
、week
),每个数据框都包含5个列(id
、station
、pluMean
、pluMax
、tsaMean
、tsaMax
)和3个观测值。图解如下:
按照站点和id进行完全连接:
- df
query1$plu$year
和dfquery1$tsa$year
- df
query1$plu$month
和dfquery1$tsa$month
- df
query1$plu$week
和dfquery1$tsa$week
或者用另一种方式表示:
- df
query1[[1]][[1]]
和dfquery1[[2]][[1]]
- df
query1[[1]][[2]]
和dfquery1[[2]][[2]]
- df
query1[[1]][[3]]
和dfquery1[[2]][[3]]
并以编程方式表达(n为大列表的总元素数):
- 使用df
query1[[i]][[1]]
,dfquery1[[i+1]][[1]]
...与dfquery1[[n]][[1]]
- 使用df
query1[[i]][[2]]
,dfquery1[[i+1]][[2]]
...与dfquery1[[n]][[2]]
- 使用df
query1[[i]][[3]]
,dfquery1[[i+1]][[3]]
...与dfquery1[[n]][[3]]
我需要通过编程实现这个操作,因为在我的真实项目中,我可能会遇到另一个包含超过2个参数元素并且每个时间间隔数据帧的4个变量列以上的大型列表。
在我的分析中,始终保持不变的是另一个大型列表的所有参数元素将始终具有相同数量的timeIntervals数据框,并且每个这些timeIntervals数据框将始终具有相同数量的观察值,并且始终共享2个完全相同名称和值(id
& station
)的列
我已经成功的部分
执行以下代码片段:
> query1Changed <- do.call(function(...) mapply(bind_cols, ..., SIMPLIFY=F), args = query1)
对数据进行排序以达到预期效果。然而,这并不是一个完美的解决方案,因为我们最终会得到重复的列名(id
和station
):
> str(query1Changed)
List of 3
$ year :'data.frame': 3 obs. of 8 variables:
..$ id : int [1:3] 1 2 3
..$ station : int [1:3] 100 101 102
..$ pluMean : num [1:3] 0.509 1.929 1.095
..$ pluMax : num [1:3] 0.0147 0.803 2.4817
..$ id1 : int [1:3] 1 2 3
..$ station1: int [1:3] 100 101 102
..$ tsaMean : num [1:3] -1.4906 -0.6847 0.0587
..$ tsaMax : num [1:3] 0.257 0.958 1.372
$ month:'data.frame': 3 obs. of 8 variables:
..$ id : int [1:3] 1 2 3
..$ station : int [1:3] 100 101 102
..$ pluMean : num [1:3] 0.665 -1.356 0.196
..$ pluMax : num [1:3] 0.503 0.234 -0.44
..$ id1 : int [1:3] 1 2 3
..$ station1: int [1:3] 100 101 102
..$ tsaMean : num [1:3] -0.685 -1.281 -0.6
..$ tsaMax : num [1:3] 0.963 0.531 -0.217
$ week :'data.frame': 3 obs. of 8 variables:
..$ id : int [1:3] 1 2 3
..$ station : int [1:3] 100 101 102
..$ pluMean : num [1:3] -0.608 -1.103 1.75
..$ pluMax : num [1:3] 0.97 0.924 3.475
..$ id1 : int [1:3] 1 2 3
..$ station1: int [1:3] 100 101 102
..$ tsaMean : num [1:3] 0.376 0.37 -0.105
..$ tsaMax : num [1:3] 1.938 0.812 0.745
我们可以添加第二个处理过程来“清理”数据,但这不是最有效的解决方案。因此,我不想使用这种解决方法。
接下来,我尝试使用dplyr full_join来完成相同的操作,但没有成功。执行以下代码:
> query1Changed <- do.call(function(...) mapply(full_join(..., by = c("station", "id")), ..., SIMPLIFY=F), args = query1)
返回以下错误:
Error in UseMethod("full_join") :
no applicable method for 'full_join' applied to an object of class "list"
那么,我应该如何编写我的full_join表达式,以便在数据帧上运行呢?
或者还有其他有效的数据转换方法吗?
我在网上找到的可能有用的内容:
我找到了相关问题,但仍然无法想出如何将它们的解决方案适应我的问题。
在stackoverflow上: - 从数据框列表合并数据框[重复] - 同时合并多个数据框 - 从map()调用中连接数据框列表 - 按索引组合列表的元素
在博客上: - 使用purrr::reduce()连接数据框列表
非常感谢您的任何帮助。希望我已经清楚地描述了我的问题。 我只是在两个月前开始使用R编程,如果解决方案很明显,请原谅我;)