首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中两个不同结构的列表的组合

R中两个不同结构的列表的组合
EN

Stack Overflow用户
提问于 2017-09-29 22:43:02
回答 3查看 553关注 0票数 1

我有vendor_list

代码语言:javascript
复制
vendor_list[57:59]
[[1]]
[1] "ibm"

[[2]]
[1] "apache"    "canonical" "apple"     "novell"   

[[3]]
[1] "gnu"    "oracle"

我有problemtype_list

代码语言:javascript
复制
problemtype_list[57:59]
[[1]]
[1] "NVD-CWE-Other"

[[2]]
[1] "NVD-CWE-Other"

[[3]]
[1] "CWE-824"

我需要将它们组合成一个数据框架,这样才能

代码语言:javascript
复制
A              B
ibm       NVD-CWE-Other
apache    NVD-CWE-Other
canonical NVD-CWE-Other
apple     NVD-CWE-Other
novelle   NVD-CWE-Other 
gnu       CWE-824
oracle    CWE-824

我见过类似的问题Combine two lists in a dataframe in R

但它给了我错误

代码语言:javascript
复制
do.call(rbind, Map(data.frame, A=problemtype_list, B=vendor_list))
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 1, 0

编辑

我的每个列表的结构

代码语言:javascript
复制
str(vendor_list)
 $ : chr "cisco"
 $ : NULL
 $ : chr [1:5] "redhat" "novell" "debian" "oracle" ...
 $ : chr [1:4] "redhat" "novell" "debian" "google"
 $ : chr [1:4] "redhat" "novell" "debian" "google"

 str(problemtype_list)
 $ : chr "CWE-254"
 $ : chr "CWE-79"
 $ : chr "NVD-CWE-Other"
 $ : chr "NVD-CWE-Other"
 $ : chr "CWE-254"
 $ : chr "CWE-189"
 $ : chr "CWE-119"
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-09-29 23:02:18

我猜你的一个列表中有一个零长度元素。

代码语言:javascript
复制
vendor_list <- list("ibm", c("apache", "canonical", "apple", "novell"), c("gnu", "oracle"))
problemtype_list <- list("NVD-CWE-Other", "NVD-CWE-Other", "CWE-824")
do.call(rbind.data.frame, Map(data.frame, A=vendor_list, B=problemtype_list))
#           A             B
# 1       ibm NVD-CWE-Other
# 2    apache NVD-CWE-Other
# 3 canonical NVD-CWE-Other
# 4     apple NVD-CWE-Other
# 5    novell NVD-CWE-Other
# 6       gnu       CWE-824
# 7    oracle       CWE-824

但是,如果我们提供一个空槽:

代码语言:javascript
复制
vendor_list[[3]] <- character(0)
vendor_list
# [[1]]
# [1] "ibm"
# [[2]]
# [1] "apache"    "canonical" "apple"     "novell"   
# [[3]]
# character(0)

..。还有一个快速测试:

代码语言:javascript
复制
any(lengths(vendor_list) == 0)
# [1] TRUE
any(lengths(problemtype_list) == 0)
# [1] FALSE

..。然后合并失败:

代码语言:javascript
复制
do.call(rbind.data.frame, Map(data.frame, A=vendor_list, B=problemtype_list))
# Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  (from pit-roads.R!8460QVH#21) : 
#   arguments imply differing number of rows: 0, 1

您可以用有意义的东西(例如,NA)替换违规条目,也可以删除它们。您使用的方法完全取决于您的使用。

替换:

代码语言:javascript
复制
vendor_list[lengths(vendor_list) == 0] <- NA
problemtype_list[lengths(problemtype_list) == 0] <- NA
do.call(rbind.data.frame, Map(data.frame, A=vendor_list, B=problemtype_list))
#           A             B
# 1       ibm NVD-CWE-Other
# 2    apache NVD-CWE-Other
# 3 canonical NVD-CWE-Other
# 4     apple NVD-CWE-Other
# 5    novell NVD-CWE-Other
# 6      <NA>       CWE-824

移走:

代码语言:javascript
复制
keepthese <- (lengths(vendor_list) > 0) & (lengths(problemtype_list) > 0)
keepthese
# [1]  TRUE  TRUE FALSE
vendor_list <- vendor_list[keepthese]
problemtype_list <- problemtype_list[keepthese]
do.call(rbind.data.frame, Map(data.frame, A=vendor_list, B=problemtype_list))
#           A             B
# 1       ibm NVD-CWE-Other
# 2    apache NVD-CWE-Other
# 3 canonical NVD-CWE-Other
# 4     apple NVD-CWE-Other
# 5    novell NVD-CWE-Other
票数 2
EN

Stack Overflow用户

发布于 2017-09-29 22:54:49

你说的代码对我不起作用-我叫pv

代码语言:javascript
复制
> v = list("ibm",c("apache","canonical","apple","novelle"),c("gnu","oracle"))

> p = list("NVD-CWE-Other","NVD-CWE-Other","CWE-824")

> p
[[1]]
[1] "NVD-CWE-Other"

[[2]]
[1] "NVD-CWE-Other"

[[3]]
[1] "CWE-824"

> v
[[1]]
[1] "ibm"

[[2]]
[1] "apache"    "canonical" "apple"     "novelle"  

[[3]]
[1] "gnu"    "oracle"

然后你的代码:

代码语言:javascript
复制
> do.call(rbind, Map(data.frame, A=p, B=v))
              A         B
1 NVD-CWE-Other       ibm
2 NVD-CWE-Other    apache
3 NVD-CWE-Other canonical
4 NVD-CWE-Other     apple
5 NVD-CWE-Other   novelle
6       CWE-824       gnu
7       CWE-824    oracle

所以也许你的数据的结构是不同的。

另一种选择是:

代码语言:javascript
复制
> do.call(rbind.data.frame,mapply(cbind,v,p))
         V1            V2
1       ibm NVD-CWE-Other
2    apache NVD-CWE-Other
3 canonical NVD-CWE-Other
4     apple NVD-CWE-Other
5   novelle NVD-CWE-Other
6       gnu       CWE-824
7    oracle       CWE-824
> 
票数 1
EN

Stack Overflow用户

发布于 2018-03-13 01:56:55

您可以使用mapply来解决问题。

df=mapply(c,vendor_list,problemtype_list),它为您提供包含列表中的值的data.frame

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46497805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档