在R的数据框中合并两个列表 [英] Combine two lists in a dataframe in R

查看:725
本文介绍了在R的数据框中合并两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个结构不同的列表:

I have two lists with different structure:

listA <- list(c("a","b","c"), c("d","e"))
listB <- list(0.05, 0.5)

listA
[[1]]
[1] "a" "b" "c"

[[2]]
[1] "d" "e"

listB
[[1]]
[1] 0.05

[[2]]
[1] 0.5

我对如何使用循环将两个列表合并到一个看起来像下面的数据框中的列表有一个想法,但是我敢肯定,这样做有更有效的方法.

I have an idea of how to use looping to combine both lists in a dataframe that looks like the one below but I'm sure that there is a more efficient way of doing this.

data.frame(A = c("a","b","c","d","e"), B = c(rep(0.05,3), rep(0.5,2)))
  A    B
1 a 0.05
2 b 0.05
3 c 0.05
4 d 0.50
5 e 0.50

推荐答案

这是另一种选择:

do.call(rbind, Map(data.frame, A=listA, B=listB))

#   A    B
# 1 a 0.05
# 2 b 0.05
# 3 c 0.05
# 4 d 0.50
# 5 e 0.50

这篇关于在R的数据框中合并两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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