在 R 中组合两个列表

发布于 2025-01-11 00:54:35 字数 341 浏览 1 评论 0原文

我有两个列表 l1l2,我想将它们组合起来看起来像预期,并使用 lapply< 获取平均值/代码>。我希望它更有效,这样我就不必每次都输入值,因为我想将其用于更大的列表。实现这一目标的更好方法是什么?

l1 <- list(1,2,3,4,5)
l2 <- list(6,7,8,9,10)

expected <- list(c(1,6), c(2,7), c(3,8), c(4,9), c(5,10))
lapply(expected, mean)

谢谢你!

I have two list l1 and l2 that I would like to combined to look like the expected, and get the mean using lapply. I would like it to be more effecient so that I don't have to type out the values everytime, because I would like to use this for a larger list. What might be a better way of accomplishing this?

l1 <- list(1,2,3,4,5)
l2 <- list(6,7,8,9,10)

expected <- list(c(1,6), c(2,7), c(3,8), c(4,9), c(5,10))
lapply(expected, mean)

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不念旧人 2025-01-18 00:54:35

我们可以使用 Map 来连接 (c) 并获取 mean

Map(c, l1, l2)
mapply(\(x, y) mean(c(x, y)), l1, l2)
[1] 3.5 4.5 5.5 6.5 7.5

或者不用在循环中执行此操作,unlist > 都list到向量,cbind矩阵并获取rowMeans

rowMeans(cbind(unlist(l1), unlist(l2)))
[1] 3.5 4.5 5.5 6.5 7.5

或者可以使用pmean 来自套件

library(kit)
pmean(unlist(l1), unlist(l2))
[1] 3.5 4.5 5.5 6.5 7.5

We may use Map to concatenate (c) and get the mean

Map(c, l1, l2)
mapply(\(x, y) mean(c(x, y)), l1, l2)
[1] 3.5 4.5 5.5 6.5 7.5

Or instead of doing this in a loop, unlist both list to a vector, cbind to a matrix and get the rowMeans

rowMeans(cbind(unlist(l1), unlist(l2)))
[1] 3.5 4.5 5.5 6.5 7.5

Or may use pmean from kit

library(kit)
pmean(unlist(l1), unlist(l2))
[1] 3.5 4.5 5.5 6.5 7.5
清泪尽 2025-01-18 00:54:35

另一种可能的解决方案:

rowMeans(do.call(rbind, Map(data.frame, A=l1, B=l2)))

#> [1] 3.5 4.5 5.5 6.5 7.5

或者使用 purrr::map2_dbl :

library(purrr)

map2_dbl(l1, l2, ~ mean(c(.x, .y)))

#> [1] 3.5 4.5 5.5 6.5 7.5

Yet another possible solution:

rowMeans(do.call(rbind, Map(data.frame, A=l1, B=l2)))

#> [1] 3.5 4.5 5.5 6.5 7.5

Or using purrr::map2_dbl:

library(purrr)

map2_dbl(l1, l2, ~ mean(c(.x, .y)))

#> [1] 3.5 4.5 5.5 6.5 7.5
国际总奸 2025-01-18 00:54:35

另一种选择是在两个列表上使用一个简单的公式(根据下面的基准,它比 pmean 稍快):

(unlist(l1) + unlist(l2))/2

# [1] 3.5 4.5 5.5 6.5 7.5

Benchmark

为了确定最快的,我创建了一个稍微快一点的公式两个列表的更大数据集,每个列表的长度为 1,000。然后,我比较了迄今为止发布的所有方法。

l1 <- map(lapply(seq(1, 1000, 1), list), 1)
l2 <- map(lapply(seq(11, 1010, 1), list), 1)

输入图片此处描述

- 代码

bm <- microbenchmark::microbenchmark(Map = lapply(Map(c, l1, l2), mean),
                               mapply = mapply(\(x, y) mean(c(x, y)), l1, l2),
                               rowMeans = rowMeans(cbind(unlist(l1), unlist(l2))),
                               pmean = pmean(unlist(l1), unlist(l2)),
                               rowMeans_Paul = rowMeans(do.call(rbind, Map(data.frame, A=l1, B=l2))),
                               map2_dbl = map2_dbl(l1, l2, ~ mean(c(.x, .y))),
                               unlist_baseR = (unlist(l1) + unlist(l2))/2,
                               times = 1000)

Another option is to use a simple formula on the two lists (which is slightly faster than pmean according to the benchmark below):

(unlist(l1) + unlist(l2))/2

# [1] 3.5 4.5 5.5 6.5 7.5

Benchmark

To determine the fastest, I created a slightly larger dataset of two lists that each have a length of 1,000. Then, I compared all of the methods posted so far.

l1 <- map(lapply(seq(1, 1000, 1), list), 1)
l2 <- map(lapply(seq(11, 1010, 1), list), 1)

enter image description here

-code

bm <- microbenchmark::microbenchmark(Map = lapply(Map(c, l1, l2), mean),
                               mapply = mapply(\(x, y) mean(c(x, y)), l1, l2),
                               rowMeans = rowMeans(cbind(unlist(l1), unlist(l2))),
                               pmean = pmean(unlist(l1), unlist(l2)),
                               rowMeans_Paul = rowMeans(do.call(rbind, Map(data.frame, A=l1, B=l2))),
                               map2_dbl = map2_dbl(l1, l2, ~ mean(c(.x, .y))),
                               unlist_baseR = (unlist(l1) + unlist(l2))/2,
                               times = 1000)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文