[R] (unclassified?) Help Question

David Winsemius dwinsemius at comcast.net
Sat Jul 16 22:42:30 CEST 2011


On Jul 16, 2011, at 4:08 PM, Lars Bishop wrote:

> 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.

What does that mean? The length of g is 10 and there are 4 unique  
values. How are we supposed to get 8 categories?

 > length(g)
[1] 10
 > length(unique(g))
[1] 4

-- 
David.

> 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.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list