[R] (unclassified?) Help Question
Lars Bishop
lars52r at gmail.com
Sat Jul 16 22:08:46 CEST 2011
Dear List,
I'd appreciate you guidance for obtaining the desired result shown
below, by combining tapply(x, g, mean) and g in the example.
Basically, I'm trying to create a vector whose values are based on the
result from tapply(x, g, mean) but that follow the pattern and length
given by the factor g. Of course I'm looking for a generic solution
(i.e, not something that just work for this particualr case).
set.seed(1)
f1 <- gl(2, 1, 10, labels=c("M", "F"))
f2 <- gl(2, 2, 10, labels=c("H", "L"))
x <- rnorm(10)
d <- data.frame(f1, f2, x)
g <- interaction(f1, f2)
tapply(x, g, mean)
> M.H F.H M.L F.L
> 0.0929451 -0.3140711 -0.1740998 1.1668028
g
> [1] M.H F.H M.L F.L M.H F.H M.L F.L M.H F.H
> Levels: M.H F.H M.L F.L
Desired result:
x
> [1] 0.0929451 -0.3140711 -0.1740998 1.1668028 0.0929451 -0.3140711 -0.1740998
> [8] 1.1668028
Thanks for any help!
Lars.
More information about the R-help
mailing list