try this:

> x <- 1:30
> names(x) <- sample(LETTERS[1:5], 30, TRUE)
>
> x
 B  B  C  E  B  E  E  D  D  A  B  A  D  B  D  C  D  E  B  D  E  B  D  A  B
B  A  B  E  B
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29 30
> x <- 1:30
> names(x) <- sample(LETTERS[1:5], 30, TRUE)
> x
 C  C  C  A  E  D  D  A  D  C  E  D  D  C  C  D  A  C  D  D  C  E  C  B  A
A  B  C  D  C
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29 30
> tapply(x, names(x), mean)
       A        B        C        D        E
16.00000 25.50000 15.00000 14.55556 12.66667
>
>



On 2/27/07, David Reiss <[EMAIL PROTECTED]> wrote:
>
> Hi,
> I apologize if this topic has been discussed - I could not figure out
> a good search phrase for this question.
>
> I have a named vector x, with multiple (duplicate) names, and I would
> like to obtain a (shorter) vector with non-duplicate names in which
> the values are the means of the values of the duplicated indexes in x.
> My best (fastest) solution to this was this code:
>
> nms <- names( x )
> x.uniq <- sapply( unique( nms ), function( i ) mean( subtracted[ nms == i
> ] ) )
>
> However, this takes forever on my beefy Mac Pro. Is there a faster way
> to this using pre-written functions in R?
>
> Thanks a lot for any advice.
> -David
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch 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.

Reply via email to