user1471980 user1471980 - 25 days ago 6
R Question

convert a list to data frame in R

I have this list:

dput(data)

structure(list(open = structure(c(NA, 135.600006, 136.759995), .Dim = c(3L,
1L), .Dimnames = list(structure(c("2016-01-01", "2016-01-04",
"2016-01-05"), .Dim = c(3L, 1L)), "IBM")), high = structure(c(NA,
135.970001, 136.889999), .Dim = c(3L, 1L), .Dimnames = list(structure(c("2016-01-01",
"2016-01-04", "2016-01-05"), .Dim = c(3L, 1L)), "IBM")), low = structure(c(NA,
134.240005, 134.850006), .Dim = c(3L, 1L), .Dimnames = list(structure(c("2016-01-01",
"2016-01-04", "2016-01-05"), .Dim = c(3L, 1L)), "IBM")), close = structure(c(NA,
135.949997, 135.850006), .Dim = c(3L, 1L), .Dimnames = list(structure(c("2016-01-01",
"2016-01-04", "2016-01-05"), .Dim = c(3L, 1L)), "IBM")), volume = structure(c(NA,
5229400L, 3924800L), .Dim = c(3L, 1L), .Dimnames = list(structure(c("2016-01-01",
"2016-01-04", "2016-01-05"), .Dim = c(3L, 1L)), "IBM")), adj.close = structure(c(NA,
130.959683, 130.863362), .Dim = c(3L, 1L), .Dimnames = list(structure(c("2016-01-01",
"2016-01-04", "2016-01-05"), .Dim = c(3L, 1L)), "IBM"))), .Names = c("open",
"high", "low", "close", "volume", "adj.close"))


I am trying to convert this list to data frame so that I can do some more calculations.

I need this data frame to look like this:

Date Open High Low Close Volume
1985-01-02 3.18 3.18 3.08 3.08 1870906


I have tried this:

do.call(rbind, data)


not able to see the columns? Any ideas?

Answer

I'll post my comment as an answer:

data2 <- setNames(do.call('cbind.data.frame', data), names(data))
data2$date <- row.names(data2)
row.names(data2) <- NULL
data2 <- cbind.data.frame(date = data2$date, data2[,-7])

        date   open   high    low  close  volume adj.close
1 2016-01-01     NA     NA     NA     NA      NA        NA
2 2016-01-04 135.60 135.97 134.24 135.95 5229400  130.9597
3 2016-01-05 136.76 136.89 134.85 135.85 3924800  130.8634

Basically, we use cbind.data.frame rather than rbind to get close to what we want. From there it's reorganizing the data.frame