[R] Loop avoidance in simulating a vector
Christos Hatzis
christos.hatzis at nuverabio.com
Thu Oct 16 22:05:31 CEST 2008
Have a look at mapply.
-Christos
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of David Afshartous
> Sent: Thursday, October 16, 2008 3:47 PM
> To: r-help at r-project.org
> Subject: [R] Loop avoidance in simulating a vector
>
>
>
> All,
>
> I'd like to simulate a vector that is formed from many
> distinct distributions and avoid a loop if possible. E.g, consider:
>
> mu = c(1, 2, 3)
> sigma = c(1, 2, 3)
> n = c(10, 10, 10)
>
> And we simulate a vector of length 30 that consists of
> N(mu[i], sigma[i])
> distributed data, each of length n[i]. Of course for just
> three groups we
> can simply write it out as:
>
> DV = c(rnorm(n[1], mu[1], sigma[1]), rnorm(n[2], mu[2],
> sigma[2]), rnorm(n[3], mu[3], sigma[3]) )
>
> For many groups we can use a loop (assuming equal numbers per group):
>
> n = n[1]
> DV = numeric(N*n)
> for (i in 1:N) {
> DV[(n*i - (n-1)): (n*i)] = rnorm(n, mu[i], sigma[i])
> }
>
> Is there any way to do the general cas without using a loop?
>
> Cheers,
> David
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list