[R] Two basic data manipulation questions (counting and aggregating)

Julien Barnier julien at no-log.org
Fri Apr 13 16:17:04 CEST 2007


Hi,

Sorry for replying to myself, but I think I have found a solution for
my first question.

> First, I would like to create a "counter" variable which will count
> the rank of each row inside each "id" level, ie something like :
>
> id     type   counter
> 10002  "7"      1 
> 10061  "1"      1
> 10061  "1"      2
> 10061  "4"      3
> 10065  "7"      1
> 10114  "1"      1 
> 10114  "1"      2
> 10114  "4"      3
> 10136  "7"      1
> 10136  "2"      2
> 10136  "2"      3

If I use :

df$counter <- unlist(tapply(df$id, df$id, order))

But there may be a better solution...

-- 
Julien



More information about the R-help mailing list