# [R] R: Simple programming question

From: Guazzetti Stefano <Stefano.Guazzetti_at_ausl.re.it>
Date: Fri, 18 May 2007 16:51:56 +0200

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,

Stefano

-----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), var3=c(8,7,6,6,5,4,5,4,3,4,3,2,3,2,1,1)) 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") -> "high"
2. the second highest value -> "mid"
3. lowest value -> "low"

This would be the output of this reasoning:

dfr\$score <-
factor(c("high","mid","low","low","high","mid","mid","low","high","mid","low","low","high","mid","low","low")) dfr

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

Cheers,
Lauri

[[alternative HTML version deleted]]

R-help_at_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.

R-help_at_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. Received on Fri 18 May 2007 - 15:19:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 May 2007 - 15:31:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.