简体   繁体   中英

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?

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

You can use a for loop to merge this list. I use as.numeric to delete row and col name of each matrix:

list.to.df <- 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"))

names <- names(list.to.df)

df <- data.frame(Date=as.Date(row.names(list.to.df[[1]])))

for(i in 1:length(list.to.df)){
  df[,i + 1] <- as.numeric(list.to.df[[i]])
  names(df)[i + 1] <- names[i]
}

        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM