首页 文章

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

提问于
浏览
6

上下文和数据结构

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

根据对该问题的最热烈回答:How to make a great R reproducible example ?,我使用 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

那怎么读?我有大列表( query1 )制成的2个参数元素( plutsa ),每个这些2个参数元件是由3个元素( yearmonthweek ),每个这些3个元素被一个一个时间间隔数据帧制成的列表的相同的4个变量列( idstationmeanmax )和完全相同的观察数量( 3 ) .

我想要实现的目标

我想 programmatically full_joinidstation 所有timeInterval数据帧具有相同的名称( yearmonthweek ) . 这意味着,我应该结束了用含3个dataframes一个新的列表( query1Changed )( yearmonthweek ),每个含有5列( idstationpluMeanpluMaxtsaMeantsaMax )和3个观察它们 . 原理上,我需要按如下方式排列数据:

do a full_join by station and id of :

  • df query1$plu$year 与df query1$tsa$year

  • df query1$plu$month 与df query1$tsa$month

  • df query1$plu$week with df query1$tsa$week

或用另一种表示形式表达:

  • df query1[[1]][[1]] 与df query1[[2]][[1]]

  • df query1[[1]][[2]] 与df query1[[2]][[2]]

  • df query1[[1]][[3]] with df query1[[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个以上参数元素的大型列表,以及每个timeIntervals数据帧中超过4个变量列 .

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

我成功了

执行以下代码:

> 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表达式以使其在数据帧上运行?

或者有其他方法可以有效地执行我的数据转换吗?

我在网上发现了什么可以提供帮助?

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

On stackoverflow : - Merging a data frame from a list of data frames [duplicate] - Simultaneously merge multiple data.frames in a list - Joining list of data.frames from map() call - Combining elements of list of lists by index

On blogs : - Joining a List of Data Frames with purrr::reduce()

任何帮助将不胜感激 . 我希望我在2个月前开始使用R编程,所以如果解决方案很明显,请放纵一下;)

1 回答

  • 4

    首先,感谢您发布了一个非常好的描述,说明您的问题是什么以及您的解决方案需要哪些要求 .

    首先,我使用 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个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
    

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

    更新:以下单行也是如此: reduce(query1, map2, full_join) . 但它并不具有可读性 .

相关问题