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
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)