Search code examples
rr-s4r-sp

Pass every element of a large list in R to a Function


In R, Let's say I have a LargeList called l with 200 elements, each of them is an S3 dataframe with 1000 - 10000 rows, named PersonXXX.

Now I want to pass every element (which is every PersonXXX) in l into a function called fungeo(data.frame). For which each of the fungeo(PersonXXX) will produce another S4:SpatialPointDataFrame. By brute force, I can do p.XXX = fungeo(PersonXXX) 200 times, but it seems not intuitive. So, I attempted to write a for loop which looks like:

p[[1]]<- 0
for (i in 1: 200){
  p[[i]]<-fungeo(l[[i]])
}

which have two problems. First of all, it actually never ran. I keep getting this error message:

   Error in p[[i]] <- fungeo(l[[i]]) : 
  more elements supplied than there are to replace

Two, I want to get my new Largelist of S4 dataframe with each element named consistently with the personID, let's say p.XXX is the S4 transformation of the simple S3 DataFrame PersonXXX.

Is there any way I can do that? Any help will be incredibly helpful!


Solution

  • You could try either the "apply" family function or "map" function from "purrr" package which should do the same job but the latter one could be much faster when working with large dataset

    lapply(s, fungeo)
    map(s,fungeo)