samedi 30 septembre 2017

Simultaneous calculation of means from a vector based on a grouping condition in R

I was wondering if there is a way to simultaneously obtain the means of k groups from y in the following R code:

k = gl(3, 5, 15)
y = as.vector(unlist(mapply(FUN = rnorm, n = rep(5, 3), mean = c(4, 5, 6))))

Here is what I have tried with no success:

mean(y[k == c(1, 2, 3)])

Aucun commentaire:

Enregistrer un commentaire