Skip to content
Prev 3202 / 15380 Next

[R-es] Media en subgrupos de grupos

Hola Jesus,

Puedes obtener resultados similares utilizando ave(), aggregate(),
tapply(), todos en base, o a/d/l*ply en la libreria plyr (ver [1]).  Otra
posibilidad es a traves de la libreria tables (ver [2])

A continuacion un ejemplo con tapply() y aggregate():

n <- 1000
sexo <- sample(c('hombre', 'mujer'), n, TRUE)
franja <- sample(c('infantil', 'adulto', 'anciano'), n, TRUE)
annos <- rnorm(n, 30, 15)
d <- data.frame(sexo, franja, annos)

with(d, aggregate(annos, list(sexo, franja), mean))
with(d, tapply(annos, list(sexo, franja), mean))

Para obtener la media global tendrias que hacer un pequeno "truco"  ;-)

Un saludo,
Jorge.-

[1]  http://www.jstatsoft.org/v40/i01/paper
 [2]  http://cran.r-project.org/web/packages/tables/index.html



2012/1/25 Jesus Armand Calejero Roman <>
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <https://stat.ethz.ch/pipermail/r-help-es/attachments/20120125/af8be29d/attachment-0001.html>
------------ próxima parte ------------
A non-text attachment was scrubbed...
Name: no disponible
Type: image/png
Size: 31938 bytes
Desc: no disponible
URL: <https://stat.ethz.ch/pipermail/r-help-es/attachments/20120125/af8be29d/attachment-0001.png>