R中

2017-09-29 61 views
1

合併兩個列表具有不同的結構我已經vendor_listR中

vendor_list[57:59] 
[[1]] 
[1] "ibm" 

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

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

而且我有problemtype_list

problemtype_list[57:59] 
[[1]] 
[1] "NVD-CWE-Other" 

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

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

我需要將它們結合起來,使數據幀,從而使得

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

但它給我的錯誤

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 

編輯

我的每一個列表的結構

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" 
+0

(我是對的。你有'vendor_list'空元素!) – r2evans

+0

其實空元素不會引發錯誤。 NULL,但是,會。你可以快速地用空字符串或任何你喜歡的東西替換那些NULL。 – Troy

回答

2

我的猜測是,你的列表中的一個具有零長度元素。

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 

然而,如果我們提供了一個空槽:

vendor_list[[3]] <- character(0) 
vendor_list 
# [[1]] 
# [1] "ibm" 
# [[2]] 
# [1] "apache" "canonical" "apple"  "novell" 
# [[3]] 
# character(0) 

...和快速測試:

any(lengths(vendor_list) == 0) 
# [1] TRUE 
any(lengths(problemtype_list) == 0) 
# [1] FALSE 

...然後合併失敗:

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),或者您可以刪除它們。您使用哪種方法完全取決於您的使用。

更換:

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 

去除:

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 
+0

它爲我工作,謝謝。請upvote我的問題 –

+0

另一個ALT堆棧(setNames(vendor_list,problemtype_list))' – user20650

+1

我知道那裏有一個更簡單的功能......謝謝,@ user20650! – r2evans

1

你說對我不起作用作品的代碼 - 我叫東西pv

> 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" 

那麼你的代碼:

> 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 

因此,也許你的數據結構不同。

或者:

> 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 
> 
+0

我會添加每個列表的str輸出。因爲我仍然收到錯誤 –

+0

請參閱編輯 –

+0

當'mapply'返回一個'array'副作爲'list'時,如果沒有'SIMPLIFY = FALSE',你可能會失敗。儘管只有在所有結果完全相同的情況下才會發生這種情況,但仍值得警惕。 – r2evans