Home > Enterprise >  How to run R code more than once for multiple n
How to run R code more than once for multiple n

Time:10-17

I have the following code in R which runs fine.

n = 2
funs = expand.grid(rep(list(1:10), n))

However I want to run this for multiple n at the same time. I want to run it for n= 2,3,4,5,6,7,8 So I tried this, but it didn't work. How should I modify my code?

n = [2:8]
funs = expand.grid(rep(list(1:10), n))

CodePudding user response:

my_fun = function(n) expand.grid(rep(list(1:10), n))
a <- lapply(2:8, my_fun)

The function produces a list of dataframes, one for each parameter value. To access the values for n = 2, the first parameter value, you could use a[1].

  •  Tags:  
  • r
  • Related