将三级列表结构转换为data.frame

时间:2013-03-20 14:55:07

标签: r dataframe nested-lists

我有一个三级列表,输入在下面。如何将其转换为data.frame?输出应为6x23 data.frame,最好使用正确的列名。行数是顶级列表的length(),列数是最低级别列表的length()

我尝试按照此处的建议执行此操作(R list to data frame):

df <- data.frame(matrix(unlist(l), nrow=6, byrow=T))

它几乎可以工作,但一切都转化为一个因素。这是因为unlist()只返回一个向量,所有内容都被强制转换为相同的数据类型。

谢谢。

list(structure(list(contract = structure(list(conId = "98333158", 
    symbol = "6823", sectype = "STK", exch = "SEHK", primary = "SEHK", 
    expiry = "", strike = "0", currency = "HKD", right = "0", 
    local = "6823", multiplier = "", combo_legs_desc = "", comboleg = "", 
    include_expired = "", secIdType = "", secId = ""), .Names = c("conId", 
"symbol", "sectype", "exch", "primary", "expiry", "strike", "currency", 
"right", "local", "multiplier", "combo_legs_desc", "comboleg", 
"include_expired", "secIdType", "secId"), class = "twsContract"), 
    portfolioValue = structure(list(position = 9000, marketPrice = 7.4949999, 
        marketValue = 67455, averageCost = 7.34352835, unrealizedPNL = 1363.24, 
        realizedPNL = 0, accountName = "U"), .Names = c("position", 
    "marketPrice", "marketValue", "averageCost", "unrealizedPNL", 
    "realizedPNL", "accountName"))), .Names = c("contract", "portfolioValue"
), class = "eventPortfolioValue"), structure(list(contract = structure(list(
    conId = "107651219", symbol = "EEM", sectype = "OPT", exch = "AMEX", 
    primary = "AMEX", expiry = "20130621", strike = "45", currency = "USD", 
    right = "C", local = "EEM   130622C00045000", multiplier = "100", 
    combo_legs_desc = "", comboleg = "", include_expired = "", 
    secIdType = "", secId = ""), .Names = c("conId", "symbol", 
"sectype", "exch", "primary", "expiry", "strike", "currency", 
"right", "local", "multiplier", "combo_legs_desc", "comboleg", 
"include_expired", "secIdType", "secId"), class = "twsContract"), 
    portfolioValue = structure(list(position = 7, marketPrice = 0.335, 
        marketValue = 234.5, averageCost = 191.78912855, unrealizedPNL = -1108.02, 
        realizedPNL = 0, accountName = "U"), .Names = c("position", 
    "marketPrice", "marketValue", "averageCost", "unrealizedPNL", 
    "realizedPNL", "accountName"))), .Names = c("contract", "portfolioValue"
), class = "eventPortfolioValue"), structure(list(contract = structure(list(
    conId = "114533598", symbol = "ESTX50", sectype = "FUT", 
    exch = "DTB", primary = "DTB", expiry = "20130621", strike = "0", 
    currency = "EUR", right = "0", local = "FESX JUN 13", multiplier = "10", 
    combo_legs_desc = "", comboleg = "", include_expired = "", 
    secIdType = "", secId = ""), .Names = c("conId", "symbol", 
"sectype", "exch", "primary", "expiry", "strike", "currency", 
"right", "local", "multiplier", "combo_legs_desc", "comboleg", 
"include_expired", "secIdType", "secId"), class = "twsContract"), 
    portfolioValue = structure(list(position = 0, marketPrice = 2639.5, 
        marketValue = 0, averageCost = 0, unrealizedPNL = 0, 
        realizedPNL = -84, accountName = "U"), .Names = c("position", 
    "marketPrice", "marketValue", "averageCost", "unrealizedPNL", 
    "realizedPNL", "accountName"))), .Names = c("contract", "portfolioValue"
), class = "eventPortfolioValue"), structure(list(contract = structure(list(
    conId = "44053851", symbol = "REM", sectype = "STK", exch = "ARCA", 
    primary = "ARCA", expiry = "", strike = "0", currency = "USD", 
    right = "0", local = "REM", multiplier = "", combo_legs_desc = "", 
    comboleg = "", include_expired = "", secIdType = "", secId = ""), .Names = c("conId", 
"symbol", "sectype", "exch", "primary", "expiry", "strike", "currency", 
"right", "local", "multiplier", "combo_legs_desc", "comboleg", 
"include_expired", "secIdType", "secId"), class = "twsContract"), 
    portfolioValue = structure(list(position = 200, marketPrice = 15.5649996, 
        marketValue = 3113, averageCost = 13.9649, unrealizedPNL = 320.02, 
        realizedPNL = 0, accountName = "U"), .Names = c("position", 
    "marketPrice", "marketValue", "averageCost", "unrealizedPNL", 
    "realizedPNL", "accountName"))), .Names = c("contract", "portfolioValue"
), class = "eventPortfolioValue"), structure(list(contract = structure(list(
    conId = "15016062", symbol = "USD", sectype = "CASH", exch = "IDEALPRO", 
    primary = "IDEALPRO", expiry = "", strike = "0", currency = "CAD", 
    right = "0", local = "USD.CAD", multiplier = "", combo_legs_desc = "", 
    comboleg = "", include_expired = "", secIdType = "", secId = ""), .Names = c("conId", 
"symbol", "sectype", "exch", "primary", "expiry", "strike", "currency", 
"right", "local", "multiplier", "combo_legs_desc", "comboleg", 
"include_expired", "secIdType", "secId"), class = "twsContract"), 
    portfolioValue = structure(list(position = 30000, marketPrice = 1.02540505, 
        marketValue = 30762.15, averageCost = 1.02867, unrealizedPNL = -97.95, 
        realizedPNL = 0, accountName = "U"), .Names = c("position", 
    "marketPrice", "marketValue", "averageCost", "unrealizedPNL", 
    "realizedPNL", "accountName"))), .Names = c("contract", "portfolioValue"
), class = "eventPortfolioValue"), structure(list(contract = structure(list(
    conId = "37928772", symbol = "USD", sectype = "CASH", exch = "IDEALPRO", 
    primary = "IDEALPRO", expiry = "", strike = "0", currency = "SGD", 
    right = "0", local = "USD.SGD", multiplier = "", combo_legs_desc = "", 
    comboleg = "", include_expired = "", secIdType = "", secId = ""), .Names = c("conId", 
"symbol", "sectype", "exch", "primary", "expiry", "strike", "currency", 
"right", "local", "multiplier", "combo_legs_desc", "comboleg", 
"include_expired", "secIdType", "secId"), class = "twsContract"), 
    portfolioValue = structure(list(position = 75000, marketPrice = 1.25079, 
        marketValue = 93809.25, averageCost = 1.2473, unrealizedPNL = 261.75, 
        realizedPNL = 0, accountName = "U"), .Names = c("position", 
    "marketPrice", "marketValue", "averageCost", "unrealizedPNL", 
    "realizedPNL", "accountName"))), .Names = c("contract", "portfolioValue"
), class = "eventPortfolioValue"))

1 个答案:

答案 0 :(得分:4)

虽然每个子列表都明确调用lapply,但您需要as.data.frame.list。然后与rbind拼凑起来。

dfr <- do.call(rbind,lapply(l,function(x) as.data.frame.list(lapply(x,as.data.frame.list))))
dim(dfr)
[1]  6 23
table(sapply(dfr,class))

 factor numeric 
     17       6