简体   繁体   English

在 R 的匹配列表中组合不同长度的元素

[英]Combining elements of different lengths in matching lists in R

I am trying to combine information from two corresponding lists, both of 4. The first list only contains 1 element for each of the 4 items, and is of the following structure:我正在尝试合并来自两个相应列表的信息,这两个列表都是 4 个。第一个列表仅包含 4 个项目中的每一个的 1 个元素,并且具有以下结构:

List of 4
 $ :'data.frame':   8640 obs. of  3 variables:
  ..$ x    : num [1:8640] -108 -108 -108 -107 -107 ...
  ..$ y    : num [1:8640] 25.9 25.9 25.9 25.9 25.9 ...
  ..$ layer: num [1:8640] 0 0 0 0 0 0 0 0 0 0 ...
 $ :'data.frame':   20520 obs. of  3 variables:
  ..$ x    : num [1:20520] -116 -116 -116 -115 -115 ...
  ..$ y    : num [1:20520] 32.9 32.9 32.9 32.9 32.9 ...
  ..$ layer: num [1:20520] 0.002 0 0 0 0 ...
 $ :'data.frame':   13500 obs. of  3 variables:
  ..$ x    : num [1:13500] -112 -112 -112 -111 -111 ...
  ..$ y    : num [1:13500] 29.9 29.9 29.9 29.9 29.9 ...
  ..$ layer: num [1:13500] 0.00583 0.01166 0.01749 0.01749 0.01749 ...
 $ :'data.frame':   15300 obs. of  3 variables:
  ..$ x    : num [1:15300] -117 -117 -117 -116 -116 ...
  ..$ y    : num [1:15300] 31.9 31.9 31.9 31.9 31.9 ...
  ..$ layer: num [1:15300] 0 0 0 0 0 0 0 0 0 0 ...

I have another list that is also of 4, where I want to add the data in that list as 2 separate columns to the dataframes in their corresponding elements in the first list.我有另一个也是 4 的列表,我想将该列表中的数据作为 2 个单独的列添加到第一个列表中相应元素的数据框中。

The structure of this second list is as follows:第二个列表的结构如下:

List of 4
 $ : chr [1:2] "green" "0.00689301"
 $ : chr [1:2] "blue" "0.01291301"
 $ : chr [1:2] "red" "0.02905452"
 $ : chr [1:2] "black" "0.00879968"

Basically, I want a new list that has the following structure in each of the 4 members of the list:基本上,我想要一个新列表,该列表的 4 个成员中的每一个都具有以下结构:

List of 4
 $ :'data.frame':   8640 obs. of  3 variables:
  ..$ x    : num [1:8640] -108 -108 -108 -107 -107 ...
  ..$ y    : num [1:8640] 25.9 25.9 25.9 25.9 25.9 ...
  ..$ layer: num [1:8640] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ color: chr [1:8640] "green" "green" "green"...
  ..$ value: chr [1:8640] "0.00689301" "0.00689301" ...
 $ :'data.frame':   20520 obs. of  3 variables:
  ..$ x    : num [1:20520] -116 -116 -116 -115 -115 ...
  ..$ y    : num [1:20520] 32.9 32.9 32.9 32.9 32.9 ...
  ..$ layer: num [1:20520] 0.002 0 0 0 0 ...
  ..$ color: chr [1:20520] "blue" "blue" "blue" ...
  ..$ value: chr [1:20520] "0.01291301" "0.01291301" ...

I have tried combining this information all at once using mapply() and c(), but that didn't give me the flexibility to change these new members from my second list from individual values into vectors of the same length as the corresponding new list.我曾尝试使用 mapply() 和 c() 一次组合这些信息,但这并没有给我灵活性,将我的第二个列表中的这些新成员从单个值更改为与相应的新列表长度相同的向量.

We may use Map我们可以使用Map

Map(function(x, y) transform(x, color = y[1], value = y[2]), lst1, lst2)
# or another way is
Map(function(x, y) {x[c("color", "value")] <- as.list(y); x}, lst1, lst2)

Or with map2或者用map2

library(purrr)
library(dplyr)
map2(lst1, lst2, ~ .x %>%
     mutate(color = .y[1], value = .y[2]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM