Use lapply inside for loop? [2]

Asked

Viewed 46 times

2

About the question in:

Use lapply inside for loop?

I need to access my matrix in parts (because the procedure generates a problem with the available memory limit for R, given the large size of my initial matrix --500 columns, size 200-). For example:

for(j in 1:100){
    set.seed(1234)
    res2 <- lapply(seq_len(nBS), function(i) apply(dados[,j,drop=F], 2, sample, n, TRUE))
}

When I do that, just the column j = 100 is used.

How do I use all vectors of my matrix in this for loop (from 1 to 100)?

Any suggestions to do this more efficiently for the 500 columns?

Thank you in advance!

  • 1
    1. Put the set.seed out of cycle for. 2) Initialize res2 <- vector("list", 100) also outside the cycle. 3) Inside the cycle, res2[[j]] <- lapply(etc).
  • 1
    1. Finally, it might be better to use the second solution of the link with function(i) apply(dados[, 1:100], etc). If you do this you don’t need to initialize the variable res2 as I said in point 2) above. It is to do exactly as it is in that response only with the difference of the dataset that will become a subset of the database dados. After processing this subset, you can repeat with dados[, 101:200] etc up to all 500 columns.
  • Thanks!! The solution 4) is great!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.