dplyr:如何以编程方式将full_join数据帧包含在列表列表中?

pok*_*yah 8 r data-manipulation dplyr tidyverse

上下文和数据结构

我将与您分享我的庞大数据集的简化版本.这个简化版本完全尊重我原始数据集的结构,但包含的列表元素,数据框,变量和观察结果比原始数据集少.

根据对这个问题的最热烈回答:如何制作一个很好的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"))
Run Code Online (Sandbox Code Playgroud)

执行此操作后,如果执行,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
Run Code Online (Sandbox Code Playgroud)

那怎么读?我有大列表(query1)由2个参数元素(plu&tsa),每个这些2个的参数元件是由3个元素(的列表year,month,week),每个这些3个元素是一个的一个时间间隔由相同的4个的数据帧的变量列(id,station,mean,max),准确地(相同数目的观测值3).

我想要实现的目标

我想编程方式 full_join通过idstation所有的一个时间间隔 dataframes具有相同的名称(year,month,week).这意味着,我应该用新的列表(最终query1Changed含有3个dataframes() , year,month),week每个含有5列(其中id,station,pluMean,pluMax,tsaMean,tsaMax)和3个观测.原理上,我需要按如下方式安排数据:

按站和id执行full_join:

  • 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]]with df query1[[i+1]][[1]]... with dfquery1[[n]][[1]]
  • df query1[[i]][[2]]with df query1[[i+1]][[2]]... with dfquery1[[n]][[2]]
  • df query1[[i]][[3]]with df query1[[i+1]][[3]]... with dfquery1[[n]][[3]]

我需要以编程方式实现这一点,因为在我的实际项目中,我可能会遇到另外一个包含2个以上参数元素的大型列表,以及每个timeIntervals数据帧中的4个以上变量列.

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

我成功了什么

执行以下代码:

> query1Changed <- do.call(function(...) mapply(bind_cols, ..., SIMPLIFY=F), args = query1)
Run Code Online (Sandbox Code Playgroud)

按预期排列数据.然而,这不是一个简洁的解决方案,因为我们最终得到重复的列名称(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
Run Code Online (Sandbox Code Playgroud)

我们可以添加第二个流程来"清理"数据,但这不是最有效的解决方案.所以我不想使用这种解决方法.

接下来,我尝试使用dplyr full_join做同样的事情,但没有成功.执行以下代码:

> query1Changed <- do.call(function(...) mapply(full_join(..., by = c("station", "id")), ..., SIMPLIFY=F), args = query1)
Run Code Online (Sandbox Code Playgroud)

返回以下错误:

Error in UseMethod("full_join") :
  no applicable method for 'full_join' applied to an object of class "list"
Run Code Online (Sandbox Code Playgroud)

那么,我应该如何编写full_join表达式以使其在数据帧上运行?

还是有另一种方法可以有效地执行我的数据转换?

我在网上发现了什么可以帮到你?

我找到了相关的问题,但我仍然无法弄清楚如何使他们的解决方案适应我的问题.

上计算器: - 从数据帧的列表合并的数据帧[复制] - 在一个列表中同时合并多个data.frames - 从地图加入data.frames的列表()调用 - 通过索引列表的列表的配合元件

在博客上: - 使用purrr :: reduce()连接数据框列表

任何帮助将不胜感激.我希望我已经清楚地描述了我的问题.我在2个月前开始使用R编程,所以如果解决方案很明显,请放纵;)

Lui*_*igo 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
Run Code Online (Sandbox Code Playgroud)

嗯,这在只有两个时有效,但您希望它在有 n 个 data.frames 列表时起作用。现在你需要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
Run Code Online (Sandbox Code Playgroud)

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

更新:下面的一行做了同样的:reduce(query1, map2, full_join)。但是,它不那么可读。