Search code examples
rloopsgamma-distribution

Trying to iterate my function many times using i


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)))
    }

Solution

  • 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)))
      }
    }