Converting a data.frame to a list of lists

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)

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)


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.


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)

Tags:

R

Dataframe