[R] Problems with computing an aggregated score

2009-07-15 Thread Hatsch

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
-- 
View this message in context: 
http://www.nabble.com/Problems-with-computing-an-aggregated-score-tp24495390p24495390.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Problems with computing an aggregated score

2009-07-15 Thread Mark Knecht
On Wed, Jul 15, 2009 at 3:37 AM, Hatschjohannes.h...@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

__
R-help@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.


Re: [R] Problems with computing an aggregated score

2009-07-15 Thread Jorge Ivan Velez
Dear Johannes,
If 'x' is your data set, here is one way using apply:

res - apply(x[,-1], 1, function(x){
  xo - sort(x, decreasing = TRUE)
  mean(xo[1:3])
  }
   )
names(res) - x[,1]
res
#   ABC
#  5.67 5.00 5.33

See ?apply, ?sort and ?mean for more information.

HTH,

Jorge


On Wed, Jul 15, 2009 at 6:37 AM, Hatsch johannes.h...@googlemail.comwrote:


 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
 --
 View this message in context:
 http://www.nabble.com/Problems-with-computing-an-aggregated-score-tp24495390p24495390.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.