Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to deal with large lists

I have a list of length 100. Each entry is itself a list containing 31 matrices with 10 rows and five columns.

>length(dati_fault)
[1] 100
> length(dati_fault[[1]])
[1] 31

I should get 100 averages of the variables summarizing the 31 matrices each element of the list

media_1<-list()
for(i in nrow(dati_fault)){
  for(j in nrow(dati_fault[[i]]))
  media_1[[i]]<-lapply(dati_fault, colMeans(j))
  
}
media_1

But I get nothing because the media_1 list remains empty

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you don’t need for loops to do so. You can directly work with lapply:

lapply(dati_fault, \(x) colMeans(do.call(rbind, x)))

This does the following: for each entry of dati_fault (i.e. each sublist of 31 matrices) these matrices are bound together (using rbind) into one single matrix with 310 rows and 5 columns. Then, colMeans is applied to this matrix.

If you are not familiar with the shorthand notation for anonymous functions (i.e. \(x)) you can read about it here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading