Repeating a user-defined function using replicate() or sapply()

前端 未结 4 1761
太阳男子
太阳男子 2021-02-01 09:52

I have defined a custom function, like this:

my.fun = function() {

      for (i in 1:1000) {
      ...
        for (j in 1:20) {
          ...
        }
      }         


        
4条回答
  •  迷失自我
    2021-02-01 10:19

    If you replace replicate with rlply from the plyr package, you can use do.call with rbind:

    library(plyr)
    do.call(rbind, rlply(5, my.fun()))
    

    If you'd rather not rely on the plyr package, you can always do:

    do.call(rbind, lapply(1:5, function(i) my.fun()))
    

提交回复
热议问题