last day (14 days later) » 

9:56 AM
0
Q: Recombining data frames from a list in R

PaulBealesI have a function that outputs a large matrix (Mat1) and a small dataframe (Smalldf1) I store them in a list called "Result". This function is run inside a loop so I create many versions of my "Result" list until my loop ends....each time I add them to a list called "FINAL". At the end of my loo...

 
If the data.frames that you need to extract are for example always the first element of the nested lists, this will work: library(dplyr); lapply(FINAL, "[[", 1) %>% bind_rows. Enter 2 instead of 1 if it's in second place
 
getting an error - Error in lapply() RHS appears to be a function name , but it cannot be found
 
Well I tried to reproduce your data like this: df <- data.frame(X1=1:3, X2=4:6, X3=7:9); FINAL=list(Result=list(Mat1=as.matrix(df), Smalldf1=df), Result=list(Mat2=as.matrix(df), Smalldf2=df)) If it's not like this please dput an extract...
 
Yes - this is what my data looks like..running your solution on this data i get this error: Error: cannot convert object to a data frame.
 
my solution extracts the first element of the nested list. If it's the matrix that is the first you should put a 2 instead of the 1... lapply(FINAL, "[[", 2) %>% bind_rows
 
9:56 AM
OK - it works on the 2nd element...it seems to work on your example just fine but for some reason i get an error on my code "RHS appears to be a function name, but it cannot be found"...I'm sorry I know you don't have my code, I can't share the output.
 
Please modify your question to include the result of this: str(head(FINAL, 2))
 
List of 2
 
hi! :-)
 
$: List of2
$ Smalldf1: 'data.frame' 1 obs of 3 variables:
$X1
$X2
$X3
$mat : List of 1
$ num[1:213,1:616]
thats what u requested
 
thanks
i don't see here any nested list? (list of list)
 
10:03 AM
List of 1
$: List of2
$ Smalldf1: 'data.frame' 1 obs of 3 variables:
$X1
$X2
$X3
$mat : List of 1
$ num[1:213,1:616]
thats its with head,1...I have 2 lists
 
ok well i'm sorry idon't see whats wrong here. i tried again...
 
ok...no worries thanks for you time..
 
wait
why is mat indicated as a list of 1, it should directly be a matrix... strange
in fact it's difficult to analyse it because it seems that you didn't paste the whole result, normally there are some ::::$ characters that enable to un derstand the structure of the levels... you don't have them? better paste it all in a r-fiddle.org/# and save it and them give me the URL
 
yeah..i'm looking at that right now, I'm doing exactly what you are doing in your example...yet I can see that it has my matrix as a list...hmmm... this is probabaly why i'm getting the error using your solution on my data. I need to figure out what going wrong upstream in my code.
yeah it has all of the .. ..$ I just didn't paste it in
doing your code it in 2 lines works..?
ie...DF1 <- lapply(FINAL, "[[", 1)...then do.call("rbind",DF1)
 
10:26 AM
ok nice. but it didn't work with the dplyr package function bind_rows? maybe you have an older version of dplyr, strange. well in one line it goes
do.call("rbind", lapply(FINAL, "[[", 1) )
it's basically the same as my original code (but probably a little slower with do.call
anyway, the problem we found concerning the matrix in a "list", should not impact our result because we only extract the data.frame
let me make it into an answer, and tell me if you still have any pb
Ok i tested it on different versions i confirm it"s a problem of dplyr version. sorry for that, i edited my answer. See you!
 
thanks for your help..i got there in the end...!
 
10:42 AM
i changed the title and retag your post to be clearer.
 

last day (14 days later) »