将一个列表的列表转换为不同排列的列表,以供pmap使用。

huangapple go评论58阅读模式
英文:

Transform a list of lists into a differently arranged list of lists to use for pmap

问题

Sure, here's the translated content:

我一直在努力使pmap()正常工作,并误解了我应该提供给它的列表的结构。

基本上,我最终得到了一个列表的列表,名为before,但它的排列方式不正确,无法用于pmap

以下是一个最小化的可重现示例:

before <- list(
  list(
    tibble(a = c(10:13), b = c(13:16), c = c(17:20)),
    tibble(d = c(10:13), e = c(13:16)),
    tibble(f = c(10:13))
  ),
  list(
    tibble(a = c(10:13), b = c(13:16), c = c(17:20)),
    tibble(d = c(21:24), e = c(25:28)),
    tibble(f = c(21:24))
  ),
  list(
    tibble(a = c(31:34), b = c(35:38), c = c(20:23)),
    tibble(d = c(31:34), e = c(35:38)),
    tibble(f = c(31:34))
  ),
  list(
    tibble(a = c(40:43), b = c(43:46), c = c(47:50)),
    tibble(d = c(40:43), e = c(43:46)),
    tibble(f = c(40:43))
  )
)

然而,这对于在pmap().l参数中使用是错误的。

我认为我想要将它转换成像下面的列表的列表,名为after

after <- list(
  list(
    tibble(a = c(10:13), b = c(13:16), c = c(17:20)),
    tibble(a = c(10:13), b = c(13:16), c = c(17:20)),
    tibble(a = c(31:34), b = c(35:38), c = c(20:23)),
    tibble(a = c(40:43), b = c(43:46), c = c(47:50))
  ),
  list(
    tibble(d = c(10:13), e = c(13:16)),
    tibble(d = c(21:24), e = c(25:28)),
    tibble(d = c(31:34), e = c(35:38)),
    tibble(d = c(40:43), e = c(43:46))
  ),
  list(
    tibble(f = c(10:13)),
    tibble(f = c(21:24)),
    tibble(f = c(31:34)),
    tibble(f = c(40:43))
  )
)

这样,我就可以执行以下操作...

x <- pmap(after, my_func) 

其中my_func是一个接受3个不同tibbles作为参数的函数。实际上,它是一个创建复杂ggplots的函数。

问题是:

  • 如何将before转换成after
  • after的形式是否正确,可以用于将其映射到一个接受3个tibble作为参数的函数中?
英文:

I've been struggling really hard to get pmap() to work and misunderstood the structure of the list that I am supposed to feed it.

Basically, I ended up with a list of lists, before, but it's not in the correct arrangement for pmap.

Here's a minimum repro of what it's like:

before &lt;- list(
  list(
    tibble(a = c(10:13), b = c(13:16), c = c(17:20)),
    tibble(d = c(10:13), e = c(13:16)),
    tibble(f = c(10:13))
  ),
  list(
    tibble(a = c(10:13), b = c(13:16), c = c(17:20)),
    tibble(d = c(21:24), e = c(25:28)),
    tibble(f = c(21:24))
  ),
  list(
    tibble(a = c(31:34), b = c(35:38), c = c(20:23)),
    tibble(d = c(31:34), e = c(35:38)),
    tibble(f = c(31:34))
  ),
  list(
    tibble(a = c(40:43), b = c(43:46), c = c(47:50)),
    tibble(d = c(40:43), e = c(43:46)),
    tibble(f = c(40:43))
  )
)

However, that's wrong for what to use as the .l argument in pmap().

I think would like to transform it into a list of lists like below, after.

after &lt;- list(
  list(
    tibble(a = c(10:13), b = c(13:16), c = c(17:20)),
    tibble(a = c(10:13), b = c(13:16), c = c(17:20)),
    tibble(a = c(31:34), b = c(35:38), c = c(20:23)),
    tibble(a = c(40:43), b = c(43:46), c = c(47:50))
  ),
  list(
    tibble(d = c(10:13), e = c(13:16)),
    tibble(d = c(21:24), e = c(25:28)),
    tibble(d = c(31:34), e = c(35:38)),
    tibble(d = c(40:43), e = c(43:46))
  ),
  list(
    tibble(f = c(10:13)),
    tibble(f = c(21:24)),
    tibble(f = c(31:34)),
    tibble(f = c(40:43))
  )
)

That way, I could perform this...

x &lt;- pmap(after, my_func) 

Where my_func is a function that takes 3 different tibbles as arguments. It's actually a function that creates complicated ggplots.

The questions are:

  • How can I transform before to after?
  • Is after in the right form to use pmap to map to a function that takes 3 tibbles as arguments?

答案1

得分: 2

Using lapply:

out &lt;- lapply(1:3, \(i) lapply(before, &quot;[[&quot;, i))

#check
identical(out, after)
# [1] TRUE
英文:

Using lapply:

out &lt;- lapply(1:3, \(i) lapply(before, &quot;[[&quot;, i))

#check
identical(out, after)
# [1] TRUE

答案2

得分: 2

使用purrr中的transpose函数:

library(purrr)

after2 <- transpose(before)

identical(after, after2)
## [1] TRUE
英文:

Use tranpose from purrr:

library(purrr)

after2 &lt;- tranpose(before)

identical(after, after2)
## [1] TRUE

答案3

得分: 1

使用 pmap 时,使用适合列表内容的参数。

before %>% purrr::pmap(\(x, y, z, k) list(x, y, z, k))
[[1]]
[[1]][[1]]
# A tibble: 4 × 3
      a     b     c
  <int> <int> <int>
1    10    13    17
2    11    14    18
3    12    15    19
4    13    16    20

[[1]][[2]]
# A tibble: 4 × 3
      a     b     c
  <int> <int> <int>
1    10    13    17
2    11    14    18
3    12    15    19
4    13    16    20
...

after 上进行测试:

identical(after, before %>% purrr::pmap(\(x, y, z, k) list(x, y, z, k)))
[1] TRUE
英文:

With pmap use arguments that fit the list content

before %&gt;% purrr::pmap(\(x, y, z, k) list(x, y, z, k))
[[1]]
[[1]][[1]]
# A tibble: 4 &#215; 3
      a     b     c
  &lt;int&gt; &lt;int&gt; &lt;int&gt;
1    10    13    17
2    11    14    18
3    12    15    19
4    13    16    20

[[1]][[2]]
# A tibble: 4 &#215; 3
      a     b     c
  &lt;int&gt; &lt;int&gt; &lt;int&gt;
1    10    13    17
2    11    14    18
3    12    15    19
4    13    16    20
...

Test on after

identical(after, before %&gt;% purrr::pmap(\(x, y, z, k) list(x, y, z, k)))
[1] TRUE

huangapple
  • 本文由 发表于 2023年5月24日 20:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定