1

I have the following List named test that I am trying to turn into a data.frame()

test <- structure(list(Feb.19.2016 = structure(list(calls = structure(list(
    Strike = c(2.5, 5, 7.5), Last = c(0.5, 0.31, 0.45), Chg = c(-0.1, 
                                                                0, 0), Bid = c(0.5, 0.1, 0), Ask = c(0.75, 0.35, 0.4), Vol = c(2L, 
                                                                                                                               5L, 6L), OI = c(273L, 38L, 12L)), .Names = c("Strike", "Last", 
                                                                                                                                                                            "Chg", "Bid", "Ask", "Vol", "OI"), row.names = c("ATNM160219C00002500", 
                                                                                                                                                                                                                             "ATNM160219C00005000", "ATNM160219C00007500"), class = "data.frame"), 
    puts = structure(list(Strike = c(2.5, 5), Last = c(0.95, 
                                                       3), Chg = c(-0.15, 0), Bid = c(0.75, 2.75), Ask = c(1.1, 
                                                                                                           3.5), Vol = c(20L, 10L), OI = c(48L, 26L)), .Names = c("Strike", 
                                                                                                                                                                  "Last", "Chg", "Bid", "Ask", "Vol", "OI"), row.names = c("ATNM160219P00002500", 
                                                                                                                                                                                                                           "ATNM160219P00005000"), class = "data.frame")), .Names = c("calls", 
                                                                                                                                                                                                                                                                                      "puts"))), .Names = "Feb.19.2016")

When I try to convert it to data.frame: df <- as.data.frame(test) I get the following error:

Error in data.frame(calls = list(Strike = c(2.5, 5, 7.5), Last = c(0.5, : arguments imply differing number of rows: 3, 2

I want to turn my list (test) into a data.frame() and let any missing fields be NA:

Desired Output:

                      Strike Last   Chg  Bid  Ask   Vol  OI  Strike  Last  Chg  Bid Ask Vol OI
ATNM160219C00002500    2.5   0.50  -0.1  0.5  0.75   2   273  2.5   0.95  -0.15 0.75 1.1  20 48
ATNM160219C00005000    5.0   0.31   0.0  0.1  0.35   5   38   5.0   3.00   0.00 2.75 3.5  10 26
ATNM160219C00007500    7.5   0.45   0.0  0.0  0.40   6   12   NA    NA     NA    NA   NA  NA NA
Rime
  • 912
  • 2
  • 11
  • 39
  • Try something like `plyr::rbind.fill(Reduce(unlist, test))`. Though the provided data set doesn't contain any missing values. – David Arenburg Aug 21 '15 at 07:35
  • 4
    you have twice the same columns without indication if your instrument is a put or a call? It's a very bad approach to represent your data... – Colonel Beauvel Aug 21 '15 at 07:35
  • and it's a duplicate if you wanna combine data.frames per column with different number of rows ...http://stackoverflow.com/questions/6988184/combining-two-data-frames-of-different-lengths – Colonel Beauvel Aug 21 '15 at 07:41
  • 1
    I strongly recommand you this representation of data: `do.call(rbind, lapply(names(test[[1]]), function(u) transform(test[[1]][[u]], type=u)))` – Colonel Beauvel Aug 21 '15 at 09:05
  • @ColonelBeauvel Seemed to work good! You can add your response below so I can accept it as an answer – Rime Aug 21 '15 at 21:06

2 Answers2

2

You can do:

do.call(rbind, lapply(names(test[[1]]), function(u) transform(test[[1]][[u]], type=u)))
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
0

Another short alternative:

df %>% 
    lapply(function(x) ifelse(is_empty(x), NA, x)) %>% 
    unlist()
cesar
  • 11
  • 2