[R] How do I generate one vector for every row of a data frame?

Bill McNeill (UW) billmcn at u.washington.edu
Fri Dec 19 06:14:40 CET 2008

I am trying to generate a set of data points from a Gaussian mixture
model.  My mixture model is represented by a data frame that looks
like this:

> gmm
  weight mean  sd
1    0.3    0 1.0
2    0.2   -2 0.5
3    0.4    4 0.7
4    0.1    5 0.3

I have written the following function that generates the appropriate data:

gmm_data <- function(n, gmm) {
	c(rnorm(n*gmm[1,]$weight, gmm[1,]$mean, gmm[1,]$sd),
		rnorm(n*gmm[2,]$weight, gmm[2,]$mean, gmm[2,]$sd),
		rnorm(n*gmm[3,]$weight, gmm[3,]$mean, gmm[3,]$sd),
		rnorm(n*gmm[4,]$weight, gmm[4,]$mean, gmm[4,]$sd))

However, the fact that my mixture has four components is hard-coded
into this function.  A better implementation of gmm_data() would
generate data points for an arbitrary number of mixture components
(i.e. an arbitrary number of rows in the data frame).

How do I do this?  I'm sure it's simple, but I can't figure it out.

Bill McNeill

More information about the R-help mailing list