1

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.

  • Something like: `replicate(B, my.function(random.data))` where `my.function` does everything. – Edward May 13 '20 at 01:26

1 Answers1

0

I finally got it. It seems like %>% doesn't get along with random processes.

This works:

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

This doesn't work:

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