R Code : Combine two lists with different structures

0 votes

This is my  vendor_list

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

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

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

And this is my problemtype_list

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

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

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

I want to make a data frame such that such that

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

I have seen a similar question. But it gives me an error.

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

EDIT

My structure of each list

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"

Dec 13, 2018 in Data Analytics by Upasana
• 8,550 points
101 views

1 answer to this question.

0 votes

You seem to be having an empty element in vendor_list. Empty elements would not throw an error. NULL, however, will. You can do a quick application to replace those NULL with empty strings or whatever you prefer.

 So, this is your list with zero-length element.

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

However, if you provide an empty slot:

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

And you can then do a quick test on it. Like so,

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

... then the merge fails:

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

You can either replace the offending entries with a Not Applicable (NA), or you can remove them. 

Replacement:

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

Then Remove it:

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

Hope this helps.

answered Dec 13, 2018 by Shubham
• 13,350 points

Related Questions In Data Analytics

0 votes
2 answers

Different data structures in R

The different data types in R are ...READ MORE

answered Aug 26 in Data Analytics by anonymous
• 32,260 points
86 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
67 views
0 votes
2 answers

Filtering R data-frame with multiple conditions

librarry(dplyr) dataset >%> filter((a ==1,b>15 ) | (a==2,b&g ...READ MORE

answered Aug 8 in Data Analytics by anonymous
8,790 views
0 votes
2 answers

Integration of R with Java

there is a package called rjava that ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 45,910 points
51 views
0 votes
3 answers

R vs MATLAB, which is better with respect to machine learning?

Hello, Both are a good programming language you ...READ MORE

answered Apr 12 in Data Analytics by SA
• 1,030 points
127 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
252 views
0 votes
1 answer
0 votes
1 answer

Machine Learning and Python Code

You can create an array called actualScore ...READ MORE

answered Dec 13, 2018 in Data Analytics by Shubham
• 13,350 points
71 views