mardi 12 mai 2020

How replicate () an apply process with random function inside in r?

I want to run several times (B times, suppose) an apply that gives me a data.frame based on a random data generated by another function. I want to do that without a for-loop (I've tried using for and it works but I want to do it with replicate() or some shorter method)

This what I want:

results=c()
for(i in 1:B) {
 results=cbind(results, lapply(random.data, function (x) main.function(x))

and it gives me a data.frame of 10xB, for example and where random.data is a list of data.frames generated by a random function and main.function(x) is a function that only admits a data.frame (no lists).

This is what I've tried:

results=sapply(random.data, function(x) main.function(x)) %>% replicate(B,.)

This runs but it doesn't do what I want, it gives a data.frame of 10xB but all columns are the same.




Aucun commentaire:

Enregistrer un commentaire