Home > Net >  How can I efficiently loop trough a list containing vectors of different length
How can I efficiently loop trough a list containing vectors of different length

Time:01-14

I have a list containing vectors of different length which I need to loop trough efficently. As an example here I want to use the print function on each element. However as I will encounter this problem repeatedly, I would like to find an efficient way to do so.

list <- list(1, c(1:4), c(3:10))

for (i in seq_along(list)) {
  for(j in seq_along(list[[i]])){
    print(list[[i]][j])
  }
}

Any help is appreciated.

CodePudding user response:

Try rapply, i.e.

rapply(list, print)
#[1] 1
#[1] 1 2 3 4
#[1]  3  4  5  6  7  8  9 10
# [1]  1  1  2  3  4  3  4  5  6  7  8  9 10

CodePudding user response:

Because you are interested in efficiency, I've added a benchmarking example to evaluate (1) your approach (2) the approach propose by @Sotos (3) an approach iterating through the list elements (vectors) using lapply and (4) an approach iterating through the list elements (elements of vectors) using lapply and sapply:

list2examine <- list(1, c(1:4), c(3:10))

benchmark("original"= {
  for (i in seq_along(list2examine)) {
    for(j in seq_along(list2examine[[i]])){
      print(list2examine[[i]][j])
    }
  }
}, "Sotos" = {rapply(list, print)},
"lapply" = {lapply(list2examine, function(x) {print(x)} )},
"lapplySapply" = {lapply(list2examine, function(x) { sapply(x, function(i) {print(i)} ) })},

replications=1000,
columns = c("test", "replications", "elapsed",
            "relative", "user.self", "sys.self"))

          test replications elapsed relative user.self sys.self
3       lapply         1000    0.10      1.0      0.10     0.00
4 lapplySapply         1000    0.32      3.2      0.33     0.02
1     original         1000    2.80     28.0      2.86     0.03
2        Sotos         1000    0.12      1.2      0.13     0.03

As you can see your approach is the slowest ("elapsed") which you've already expected. lapplySapply is slower than rapply, I'll guess that has to do something with the fact that rapply is a recursive function. If you want to read further on benchmarking or how to interpret the result of the function I recommend the article https://www.r-bloggers.com/2017/05/5-ways-to-measure-running-time-of-r-code/.

Note that I've changed the name of you list because variable name should not be the same as built-in functions of r.

  •  Tags:  
  • Related