Trying to iterate my function many times using i

十年热恋 提交于 2020-01-25 07:05:18

问题


I'm trying to iterate a for loop many times but it's only printing out once.

sample_gamma <- function(alpha, beta, n, iter) {
  mean_s = c()
  mean_sd = c()
  for(i in 1:iter){
    a = rgamma(n, shape = alpha, scale = 1/beta) 
    return(c(mean(a), sd(a)))
    }

回答1:


i think you are looking for this

sample_gamma <- function(alpha, beta, n, iter) {
  mean_s = c()
  mean_sd = c()
  for(i in 1:iter){
    a = rgamma(n, shape = alpha, scale = 1/beta) 
    print(c(mean(a), sd(a)))
  }
}


来源:https://stackoverflow.com/questions/58955845/trying-to-iterate-my-function-many-times-using-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!