kevinykuo
kevinykuo

Reputation: 4772

Generating list of random vectors of random lengths

Suppose I have a vector of 'frequencies', like

freq <- rpois(N, lambda)

For each of the N simulation runs, I want to create a random vector of length freq[i] with uniform(0,1) distribution.

I naively try runif(freq, 0, 1), but R takes that to be runif(length(freq),0,1)

To maximize performance, I want to vectorize the code as much as possible. What do?

Upvotes: 1

Views: 83

Answers (1)

Christopher Louden
Christopher Louden

Reputation: 7592

lapply() will perform the task for each frequency.

freq <- rpois(N, lambda)
lapply(freq, runif)

Upvotes: 2

Related Questions