0

I have a triple level list, the dput is below. How can I convert this into a data.frame? Output should be a 6x23 data.frame and preferably with the correct column names. The number of rows is the length() of the top level list and the number of columns is combined length() of the lower-most level lists.

I tried doing this as suggested here (R list to data frame):

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

and it almost works, but everything is converted to a factor. This is because unlist() only returns a vector and everything is forced into the same data type.

Thank you.

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"))
Community
  • 1
  • 1
mchangun
  • 9,814
  • 18
  • 71
  • 101
  • 1
    Dint notice that in your answer. deleted mine. Will repost if I could think of something better. – Arun Mar 20 '13 at 15:14
  • The matrix call will coerce to a common mode, "character", and then the data.frame call will coerce to factor unless stringsAsFactors is set to FALSE. – IRTFM Mar 20 '13 at 15:23
  • @Dwin Thanks - settings stringsAsFactors creates a data.frame with everything as a "char" as you mentioned. Can I do this operation without using `unlist` and `matrix`? These are the two operations causing me issues by coercing everything into a common mode. – mchangun Mar 20 '13 at 15:27
  • @mchangun looks like you are using `IBrokers` to get your account informations.. try this function `twsPortfolioValue` – CHP Mar 20 '13 at 15:28
  • @geektrader That is indeed what I am doing. How does `twsPortfolio` work? Doing `twsPortfolioValue(tws)` gives me an error `Error in UseMethod("twsPortfolioValue") : no applicable method for 'twsPortfolioValue' applied to an object of class "c('twsconn', 'environment')"` – mchangun Mar 20 '13 at 15:32
  • 1
    try `twsPortfolioValue(reqAccountUpdates(tws), zero.pos=TRUE, ...)` – CHP Mar 20 '13 at 15:34

1 Answers1

4

You need to lapply though each sub-list calling as.data.frame.list explicitly. Then piece together with 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 
James
  • 65,548
  • 14
  • 155
  • 193
  • 1
    if OP doesn't want characters to be converted to factors then perhaps you should add `dfr <- do.call(rbind,lapply(l,function(x) as.data.frame.list(lapply(x,as.data.frame.list,stringsAsFactors=FALSE))))` – CHP Mar 20 '13 at 16:10