1

I've generated a list of lists containing data for manual review. The sublists are a combination of text and numeric data, however they are all being treated as character data by R. Each sublist holds a different number of values. For example

list_of_lists[[1]]

might return

[[1]]
[1] "company name" "company name" "29" "30"

while

list_of_lists[[2]]

might return

[[2]]
[1] "company name" "company name" "company name"
[4] "1253"         "4980"         "4981"

There are 359 sublists inside my list of lists, and I'd like to export them all to excel for manual review. The problem I'm running into is converting my list of lists to a dataframe that I can export to excel, specifically due to the fact that all my lists are different lengths. I don't need to maintain data types or anything like that, just some help getting the data to excel while maintaining readability.

Thank you!

  • 2
    For the example showed, what is the expectd output – akrun Feb 07 '20 at 17:03
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 07 '20 at 17:05
  • when you say 'and I'd like to export them all' -- you want a separate dataframe for each sublist, or you want them all in one dataframe? – novica Feb 07 '20 at 17:20

1 Answers1

0

If your data looks like

list_of_lists <- list(
  c("company name", "company name", "29", "30"),
  c("company name", "company name", "company name", "1253", "4980", "4981")
)

then you can try

do.call(rbind, lapply(list_of_lists, function(l) matrix(l, ncol=2)))
#      [,1]           [,2]  
# [1,] "company name" "29"  
# [2,] "company name" "30"  
# [3,] "company name" "1253"
# [4,] "company name" "4980"
# [5,] "company name" "4981"

to start lumping them together.

I've assumed:

  1. everything occurs in pairs, always an even number of elements, so you add all labels first, then paired with their values; and
  2. since everything is a string, then the matrix at the end is not a problem; you can always do as.data.frame, rename columns, and then convert the second column to numeric/integer if applicable.
r2evans
  • 141,215
  • 6
  • 77
  • 149