R Code Combine two lists with different structures

+1 vote

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,620 points
1,522 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,490 points

Related Questions In Data Analytics

+1 vote
2 answers

Different data structures in R

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

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
1,697 views
0 votes
1 answer

Big Data transformations with R

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

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,047 views
+1 vote
3 answers

Filtering R data-frame with multiple conditions

You can use the 'filter' function from ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
87,190 views
0 votes
2 answers

Integration of R with Java

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

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,350 points
991 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, 2019 in Data Analytics by SA
• 1,090 points
1,548 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,059 views
+2 votes
1 answer
+1 vote
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,490 points
950 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP