I am trying to generate many samples from normal distribution with different parameters (parametrs in a list).
How can I do this using apply family?
For example I need 2 samples one: (n = 10, mean = 2, sd = 3), and the second: (n = 100, mean = 0, sd = 1).
My code is not working.
lista <- list (c(10,2,3), c(100,0,1))
lapply(lista, rnorm, n=lista[[1]][1], mean=lista[[1]][2], sd=lista[[1]][3])
Please, help me :(
We can use lapply(lista, function (u) rnorm(u[1], u[2], u[3]))
.
If you are not sure how to write an lapply
, always start from writing a for
loop. lapply(lst, FUN)
is nominally a loop of this kind:
z <- vector("list", length(lst)) ## set up a list to hold result
for (i in 1:length(lst)) z[[i]] <- FUN(lst[[i]])
Now it should be clear that you want to do something like
FUN <- function (u) rnorm(u[1], u[2], u[3])