英文:
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 <- 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 <- 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 <- 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
toafter
? - Is
after
in the right form to usepmap
to map to a function that takes 3 tibbles as arguments?
答案1
得分: 2
Using lapply:
out <- lapply(1:3, \(i) lapply(before, "[[", i))
#check
identical(out, after)
# [1] TRUE
英文:
Using lapply:
out <- lapply(1:3, \(i) lapply(before, "[[", 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 <- 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 %>% 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
...
Test on after
identical(after, before %>% purrr::pmap(\(x, y, z, k) list(x, y, z, k)))
[1] TRUE
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论