[R] R: Simple programming question

Guazzetti Stefano Stefano.Guazzetti at ausl.re.it
Fri May 18 16:51:56 CEST 2007

try also this

dfr$score<-factor(dfr$var3 %in% sort(unique(dfr$var3), decr=T)[1:2] * dfr$var3,
                   labels=c("low", "mid", "high"))
Hope this helps, 


-----Messaggio originale-----
Da: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch]Per conto di Lauri Nikkinen
Inviato: venerdì 18 maggio 2007 15.15
A: r-help at stat.math.ethz.ch
Oggetto: [R] Simple programming question

Hi R-users,

I have a simple question for R heavy users. If I have a data frame like this

dfr <- data.frame(id=1:16, categ=rep(LETTERS[1:4], 4),
dfr <- dfr[order(dfr$categ),]

and I want to score values or points in variable named "var3" following this
kind of logic:

1. the highest value of var3 within category (variable named "categ") ->
2. the second highest value -> "mid"
3. lowest value -> "low"

This would be the output of this reasoning:

dfr$score <-

The question is how I do this programmatically in R (i.e. if I have 2000
rows in my dfr)?

I appreciate your help!


	[[alternative HTML version deleted]]

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list