0

So, I have created a list (and a single column matrix) that contains 256 nested lists. What I would like to do, is to convert each of the 256 lists into a single dataframe of 16 columns and then write.table it. Although each list contains the same number of columns (16), the number of rows for each list varies. I have tried to use unlist unsuccessfully because the changing row counts. I can subset each list individually, so I know there's an easier way to do the whole list.

I'm pretty new to R, so I apologize for asking what may be a naive novice question. I searched through a lot of topics the last couple days and didn't see anything that seemed to match my problem. for loop seems like it might be unnecessary and I wasn't sure if lapply was the correct route, either.

UPDATE: dput of first list:

list(structure(list(structure(c(2L, 11L, 15L, 8L, 7L, 3L, 6L,  10L,
1L, 1L, 18L, 13L, 14L, 19L, 16L, 17L, 4L, 5L, 9L, 12L), .Label = c("",
"Aaron Rodgers", "Andrew Quarless", "Derrick Coleman", "Doug Baldwin",
"DuJuan Harris", "Eddie Lacy", "James Starks", "Jermaine Kearse", 
"John Kuhn", "Jordy Nelson", "Luke Willson", "Marshawn Lynch",  "Percy
Harvin", "Randall Cobb", "Ricardo Lockette", "Robert Turbin", 
"Russell Wilson", "Zach Miller"), class = "factor"), Tm =
structure(c(1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 4L, 3L, 3L, 3L, 3L,
3L, 3L, 3L,  3L, 3L, 3L), .Label = c("GNB", "Passing", "SEA", "Tm"),
class = "factor"), Cmp = structure(c(3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 4L, 
     2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "19", 
     "23", "Cmp", "Rushing"), class = "factor"), Att = structure(c(3L, 
     1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 4L, 2L, 1L, 1L, 1L, 1L, 1L, 
     1L, 1L, 1L, 1L), .Label = c("", "28", "33", "Att", "Receiving"
     ), class = "factor"), Yds = structure(c(2L, 1L, 1L, 1L, 1L, 
     1L, 1L, 1L, NA, 4L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
     ), .Label = c("", "189", "191", "Yds"), class = "factor"), 
     TD = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 4L, 
     3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "1", 
     "2", "TD"), class = "factor"), Int = structure(c(3L, 1L, 
     1L, 1L, 1L, 1L, 1L, 1L, NA, 4L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
     1L, 1L, 1L), .Label = c("", "0", "1", "Int"), class = "factor"), 
     Lng = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 4L, 
     3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "23", 
     "33", "Lng"), class = "factor"), Att = structure(c(1L, 1L, 
     1L, 7L, 3L, 1L, 2L, 2L, NA, 8L, 7L, 4L, 5L, 1L, 1L, 6L, 1L, 
     1L, 1L, 1L), .Label = c("", "1", "12", "20", "4", "6", "7", 
     "Att"), class = "factor"), Yds = structure(c(1L, 1L, 1L, 
     7L, 6L, 1L, 9L, 3L, NA, 10L, 5L, 2L, 8L, 1L, 1L, 4L, 1L, 
     1L, 1L, 1L), .Label = c("", "110", "2", "27", "29", "34", 
     "37", "41", "7", "Yds"), class = "factor"), TD = structure(c(1L, 
     1L, 1L, 2L, 2L, 1L, 2L, 3L, NA, 5L, 2L, 4L, 2L, 1L, 1L, 2L, 
     1L, 1L, 1L, 1L), .Label = c("", "0", "1", "2", "TD"), class = "factor"), 
     Lng = structure(c(1L, 1L, 1L, 2L, 4L, 1L, 8L, 6L, NA, 9L, 
     3L, 7L, 5L, 1L, 1L, 8L, 1L, 1L, 1L, 1L), .Label = c("", "12", 
     "13", "15", "16", "2", "21", "7", "Lng"), class = "factor"), 
     Rec = structure(c(1L, 7L, 5L, 3L, 4L, 4L, 1L, 1L, NA, 8L, 
     1L, 2L, 6L, 4L, 3L, 1L, 2L, 4L, 2L, 2L), .Label = c("", "1", 
     "2", "3", "6", "7", "9", "Rec"), class = "factor"), Yds = structure(c(1L, 
     12L, 9L, 3L, 3L, 6L, 1L, 1L, NA, 13L, 1L, 4L, 10L, 8L, 7L, 
     1L, 5L, 4L, 11L, 2L), .Label = c("", "1", "11", "14", "15", 
     "26", "38", "42", "58", "59", "8", "83", "Yds"), class = "factor"), 
     TD = structure(c(1L, 2L, 3L, 2L, 2L, 2L, 1L, 1L, NA, 4L, 
     1L, 2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 2L), .Label = c("", "0", 
     "1", "TD"), class = "factor"), Lng = structure(c(1L, 7L, 
     9L, 3L, 4L, 8L, 1L, 1L, NA, 14L, 1L, 5L, 11L, 10L, 11L, 1L, 
     6L, 12L, 13L, 2L), .Label = c("", "1", "11", "12", "14", 
     "15", "16", "18", "23", "24", "33", "6", "8", "Lng"), class = "factor")), .Names = c("",  "Tm", "Cmp", "Att", "Yds", "TD", "Int",
 "Lng", "Att", "Yds",  "TD", "Lng", "Rec", "Yds", "TD", "Lng"),
 row.names = c(NA, -20L ), class = "data.frame"))

So, each observation in my list is like this above and I want to convert all of the lists into their 16 column(Now that I think about it, it's 17 columns, one is just unnamed) dataframe layout and stack all the rows together in one place that I can then write.table

Jaap
  • 81,064
  • 34
  • 182
  • 193
SFC K
  • 15
  • 2
  • 6
  • 4
    Since you're asking how to reshape a data structure, it's really tough to help without actually having an example of that data structure (note that the code you have included in the question can't be run in an R terminal). Could you please updated your question to include a reproducible example? One easy approach to doing so would be to paste the output of `dput` for a reduced dataset, as described here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – josliber Sep 29 '15 at 17:55

1 Answers1

2

Let's call your list l where l[[1]] is what you have dput above.

Two easy ways from base R and from data.table

do.call("rbind", l)
data.table::rbindlist(l)

This assumes that the columns match in each list element. Your example doesn't confirm this, although you state it.

alexwhitworth
  • 4,839
  • 5
  • 32
  • 59
  • 1
    Thanks, Alex. I did have a small inconsistency re: numbers of columns, but `data.table::rbindlist(1, fill = TRUE)` worked great. – SFC K Sep 29 '15 at 21:18