[R] Problems with computing an aggregated score

Mark Knecht markknecht at gmail.com
Wed Jul 15 17:14:11 CEST 2009


On Wed, Jul 15, 2009 at 3:37 AM, Hatsch<johannes.heer at googlemail.com> wrote:
>
> Hi all,
>
> I have a problem with computing a new variable as an aggregated score of
> other variables.
>
> Say, I have 10 variables for 1,000 observations (people). Every variable may
> have values between 0 and 8. What I would like to do is computing the mean
> of the individual top 3 values for every person.
>
> Exampe: The values for the 10 variables (v1 to v10) for person A, B and C
> are as follows:
>
> A 0 1 0 2 5 8 3 0 4 0
> B 6 4 3 0 0 0 0 5 0 0
> C 0 0 8 0 0 8 0 0 0 0
>
> So, I would like to compute a new variable for the mean of the individual
> top 3 values, which would result for person A, B and C in the following:
>
> A (8+5+4)/3 = 5.67
> B (6+5+4)/3 = 5
> C (8+8+0)/3 = 5.33
>
> Is there any way to do this?
>
> Any clues, hints and suggestions are highly appreciated,
> many thanks in advance
> Johannes

Unquestionably the experienced guys are going to give you a better
answer, but as a newbie I worked out a step by step answer in a few
minutes. This allows you to see the steps:

A <- c(0, 1, 0, 2, 5, 8, 3, 0, 4, 0)
A[order(as.numeric(A), decreasing=TRUE)]
A[order(as.numeric(A), decreasing=TRUE)][1:3]
sum(A[order(as.numeric(A), decreasing=TRUE)][1:3])
sum(A[order(as.numeric(A), decreasing=TRUE)][1:3])/3

Hope this helps,
Mark




More information about the R-help mailing list