# [R] Simple programming question

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Fri May 18 15:37:03 CEST 2007

```try 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), ]

dfr\$score <- unlist(tapply(dfr\$var3, dfr\$categ, function (x) {
sn <- sort(unique(x), decreasing = TRUE)
labs <- c("high", "mid", rep("low", length(sn) - 2))
labs[match(x, sn)]
}))

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm

----- Original Message -----
From: "Lauri Nikkinen" <lauri.nikkinen at iki.fi>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, May 18, 2007 3:15 PM
Subject: [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