我有以下名为test
的列表,我试图将其转换为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")
当我尝试将其转换为data.frame
时:df <- as.data.frame(test)
我收到以下错误:
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
我想将我的列表(test
)转换为data.frame()
,并将任何缺少的字段设为NA:
期望的输出:
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
答案 0 :(得分:1)
你可以这样做:
do.call(rbind, lapply(names(test[[1]]), function(u) transform(test[[1]][[u]], type=u)))