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 # A B C # 5.666667 5.000000 5.333333
See ?apply, ?sort and ?mean for more information. HTH, Jorge On Wed, Jul 15, 2009 at 6:37 AM, Hatsch <johannes.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 > -- > 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.