dplyr: 如何以编程方式将包含在列表中的数据框完全连接起来?

9

背景和数据结构

我将分享我的海量数据集的简化版本。这个简化版完全遵循了原始数据集的结构,但包含的列表元素、数据框、变量和观测值比原始数据集少。

根据这个问题得到的最受赞同的回答:如何创建一个优秀的 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个参数元素(plutsa)组成的大列表query1),每个这两个参数元素都是一个列表,由3个元素(yearmonthweek)组成,每个这3个元素又是由相同的4个变量列(idstationmeanmax)和完全相同数量的观测值(3)组成的timeInterval数据框。

我想要实现什么

我想要通过idstationfull_join将所有具有相同名称(yearmonthweek)的timeInterval数据框进行编程连接。full_join结果将得到一份新列表(query1Changed),其中包含3个数据框(yearmonthweek),每个数据框都包含5个列(idstationpluMeanpluMaxtsaMeantsaMax)和3个观测值。图解如下:

按照站点和id进行完全连接:

  • dfquery1$plu$year和dfquery1$tsa$year
  • dfquery1$plu$month和dfquery1$tsa$month
  • dfquery1$plu$week和dfquery1$tsa$week

或者用另一种方式表示:

  • dfquery1[[1]][[1]]和dfquery1[[2]][[1]]
  • dfquery1[[1]][[2]]和dfquery1[[2]][[2]]
  • dfquery1[[1]][[3]]和dfquery1[[2]][[3]]

并以编程方式表达(n为大列表的总元素数):

  • 使用df query1[[i]][[1]],df query1[[i+1]][[1]]...与df query1[[n]][[1]]
  • 使用df query1[[i]][[2]],df query1[[i+1]][[2]]...与df query1[[n]][[2]]
  • 使用df query1[[i]][[3]],df query1[[i+1]][[3]]...与df query1[[n]][[3]]

我需要通过编程实现这个操作,因为在我的真实项目中,我可能会遇到另一个包含超过2个参数元素并且每个时间间隔数据帧的4个变量列以上的大型列表

在我的分析中,始终保持不变的是另一个大型列表的所有参数元素将始终具有相同数量的timeIntervals数据框,并且每个这些timeIntervals数据框将始终具有相同数量的观察值,并且始终共享2个完全相同名称和值(id & station)的列

我已经成功的部分

执行以下代码片段:

> query1Changed <- do.call(function(...) mapply(bind_cols, ..., SIMPLIFY=F), args = query1)

对数据进行排序以达到预期效果。然而,这并不是一个完美的解决方案,因为我们最终会得到重复的列名(idstation):

> 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编程,如果解决方案很明显,请原谅我;)

1个回答

6
首先,感谢您发布了一个非常好的问题描述,并说明了您解决问题所需的要求。
我建议使用purrr::map2创建一个函数,该函数接受两个数据框列表,并以并行方式将它们连接起来。也就是说,它将plu的第一个数据框与tsa的第一个数据框连接起来...将plu的最后一个数据框与tsa的最后一个数据框连接起来,并将结果作为列表返回。
> join_each = function(x, y) map2(x, y, full_join)
> join_each(query1$plu, query1$tsa)
Joining, by = c("id", "station")
Joining, by = c("id", "station")
Joining, by = c("id", "station")
$year
  id station  pluMean     pluMax     tsaMean    tsaMax
1  1     100 0.509069 0.01469622 -1.49060722 0.2573984
2  2     101 1.928665 0.80298439 -0.68473542 0.9576348
3  3     102 1.095175 2.48170762  0.05866559 1.3719802

$month
  id station    pluMean     pluMax    tsaMean     tsaMax
1  1     100  0.6649385  0.5034246 -0.6846687  0.9629169
2  2     101 -1.3559339  0.2344025 -1.2808785  0.5307734
3  3     102  0.1956006 -0.4402645 -0.6001755 -0.2171436

$week
  id station    pluMean    pluMax    tsaMean    tsaMax
1  1     100 -0.6082958 0.9696683  0.3764817 1.9383364
2  2     101 -1.1025692 0.9244263  0.3704359 0.8117675
3  3     102  1.7498401 3.4746087 -0.1053549 0.7449325

这个方法在只有两个数据框时可以使用,但是如果有n个数据框,你需要使用purrr::reduce

> reduce(query1, join_each)
Joining, by = c("id", "station")
Joining, by = c("id", "station")
Joining, by = c("id", "station")
$year
  id station  pluMean     pluMax     tsaMean    tsaMax
1  1     100 0.509069 0.01469622 -1.49060722 0.2573984
2  2     101 1.928665 0.80298439 -0.68473542 0.9576348
3  3     102 1.095175 2.48170762  0.05866559 1.3719802

$month
  id station    pluMean     pluMax    tsaMean     tsaMax
1  1     100  0.6649385  0.5034246 -0.6846687  0.9629169
2  2     101 -1.3559339  0.2344025 -1.2808785  0.5307734
3  3     102  0.1956006 -0.4402645 -0.6001755 -0.2171436

$week
  id station    pluMean    pluMax    tsaMean    tsaMax
1  1     100 -0.6082958 0.9696683  0.3764817 1.9383364
2  2     101 -1.1025692 0.9244263  0.3704359 0.8117675
3  3     102  1.7498401 3.4746087 -0.1053549 0.7449325

它计算join_each(query1[[1]], query1[[2]]) %>% join_each(query1[[3]]) ... %>% join_each(query1[[n]])

更新: 下面这个一行代码也可以实现同样的功能:reduce(query1, map2, full_join)。不过它的可读性较差。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接