Converting a data.frame to a list of lists

前端 未结 5 2076
梦如初夏
梦如初夏 2020-12-09 20:50

How can I convert a data.frame

df <- data.frame(id=c(\"af1\", \"af2\"), start=c(100, 115), end=c(114,121))

To a list of lists

         


        
相关标签:
5条回答
  • 2020-12-09 21:03

    If, like me, you are mostly looking to create lists of lists to use in highcharter, that same package contains the function list_parse() (or list_parse2() if you want to remove names). Simply use it like so:

    library(highcharter)
    
    df <- data.frame(id=c("af1", "af2"), start=c(100, 115), end=c(114,121))
    
    LoL <- list_parse(df)
    

    After which you can do the indexing you wanted:

    > LoL[[1]]$start
    [1] 100
    
    0 讨论(0)
  • 2020-12-09 21:15

    In base R, it's quite a bit faster to use mapply instead of split or lapply - however, you have to invoke it via do.call so that each column is used independently.

    df <- sleep
    
    f <- function(df) {
      lapply(seq_len(nrow(df)), function(row) {
        df[row, , drop = FALSE]
      })
    }
    
    f2 <- function(df) {
      do.call("mapply", c(list, df, SIMPLIFY = FALSE, USE.NAMES=FALSE))
    }
    
    f3 <- function(df) {
      split(df, seq(nrow(df)))
    }
    
    microbenchmark::microbenchmark(f(df), f2(df), f3(df))
    #> Unit: microseconds
    #>    expr     min       lq     mean   median       uq       max neval
    #>   f(df) 573.799 607.8375 759.1721 626.0095 752.9465  2861.961   100
    #>  f2(df) 114.819 123.5190 155.5185 129.9210 141.4340  1375.573   100
    #>  f3(df) 598.774 625.6025 813.6837 634.5855 684.3825 11230.678   100
    

    Created on 2019-10-09 by the reprex package (v0.3.0)

    0 讨论(0)
  • 2020-12-09 21:24
    LMAo <- lapply(split(df,df$id), function(x) as.list(x)) # is one way
    
    # more succinctly
    # LMAo <- lapply(split(df,df$id), as.list)
    

    An edited solution as per your comment:

    lapply( split(df,seq_along(df[,1])), as.list)
    
    0 讨论(0)
  • 2020-12-09 21:26

    You can use apply to turn your data frame into a list of lists like this:

    LoL <- apply(df,1,as.list)
    

    However, this will change all your data to text, as it passes a single atomic vector to the function.

    0 讨论(0)
  • 2020-12-09 21:27

    Using plyr , you can do this

    dlply(df,.(id),c)
    

    To avoid grouping by id , if there are multiple ( maybe you need to change column name , id is unique for me)

    dlply(df,1,c)
    
    0 讨论(0)
提交回复
热议问题