rloopsvectorparametersreplicate

Separating Parameters in Repeated Function Calls


With a vector of values, I want each value to be called on a function

values = 1:10
rnorm(100, mean=values, sd=1)

mean = values repeats the sequence (1,2,3,4,5,6,7,8,9,10). How can I get a matrix, each with 100 observations and using a single element from my vector? ie:

rnorm(100, mean=1, sd=1)
rnorm(100, mean=2, sd=1)
rnorm(100, mean=3, sd=1)
rnorm(100, mean=4, sd=1)
# ...

Solution

  • It's not clear from your question, but I took it that you wanted a single matrix with 10 rows and 100 columns. That being the case you can do:

    matrix(rnorm(1000, rep(1:10, each = 100)), nrow = 10, byrow = TRUE)
    

    Or modify akrun's answer by using sapply instead of lapply