Convert data.frame file to list of lists

How to convert the data.frame file

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

To the list of lists

 LoL <- list(list(id="af1", start=100, end=114), list(id="af2", start=115, end=121)) 

I tried things like

 not.LoL <- as.list(as.data.frame(t(df))) 

and I'm really not sure what I can do after that, but this is not entirely correct. My requirement is that I can access, say, the first start using the command

 > LoL[[1]]$start [1] 100 

the not.LoL that I am not.LoL now causes the following error:

 > not.LoL[[1]]$start Error in not.LoL[[1]]$start : $ operator is invalid for atomic vectors 

Concepts and / or solutions would be greatly appreciated.

Edit: I should have clearly indicated that the "id" is not actually unique here - there may be several elements under the same identifier. Therefore, I could do this with a solution that is independent of unique identifiers before split on.

+7
source share
4 answers

Using plyr you can do it

 dlply(df,.(id),c) 

To avoid grouping by id, if there are several of them (maybe you need to change the column name, id is unique to me)

 dlply(df,1,c) 
+7
source
 LMAo <- lapply(split(df,df$id), function(x) as.list(x)) # is one way # more succinctly # LMAo <- lapply(split(df,df$id), as.list) 

Edited solution according to your comment:

 lapply( split(df,seq_along(df[,1])), as.list) 
+6
source

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

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

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

+5
source

In the R database, it is much faster to use mapply instead of split or lapply - however, you must call it through 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
source

All Articles