在R中嵌套列表的元素上逐个元素地添加列表 [英] append a list element-wise to elements of a nested list in R

查看:133
本文介绍了在R中嵌套列表的元素上逐个元素地添加列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手,但我仍然想让自己绕过apply系列,而不是使用循环.

I'm new to R and still trying to get my head around the apply family instead of using loops.

我有两个列表,一个嵌套,另一个不嵌套,都由字符组成:

I have two lists, one nested, the other not, both composed of characters:

>lst1 <- list(c("ABC", "DEF", "GHI"), c("JKL", "MNO", "PQR"))
>lst2 <- c("abc", "def")

我想创建第三个列表,以便将lst2的每个元素附加为lst1中各个子列表的最后一个元素.所需的输出如下所示:

I would like to create a third list such that each element of lst2 is appended as the last element of the respective sublist in lst1. The desired output looks like this:

>lst3
[[1]]
[1] "ABC" "DEF" "GHI" "abc"

[[2]]
[1] "JKL" "MNO" "PQR" "def"

到目前为止,我在R中的经验告诉我,可能有一种无需明确编写循环即可完成此操作的方法.

My experience thus far in R tells me there likely is a way of doing this without writing a loop explicitly.

推荐答案

您可以使用Map来完成mapply(..., simplify = F)的作用:

You can use Map which does exactly what mapply(..., simplify = F) do:

Map(c, lst1, lst2)
[[1]]
[1] "ABC" "DEF" "GHI" "abc"

[[2]]
[1] "JKL" "MNO" "PQR" "def"

这篇关于在R中嵌套列表的元素上逐个元素地添加列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆