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

Julien Barnier julien at no-log.org
Fri Apr 13 13:07:42 CEST 2007


Dear R users,

I hav two basic data manipulations questions that I can't resolve.

My data is a data frame which look like the following :

id     type
10002  "7"
10061  "1"
10061  "1"
10061  "4"
10065  "7"
10114  "1"
10114  "1"
10114  "4"
10136  "7"
10136  "2"
10136  "2"


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

Is there a straightforward way to do that, without using several
"for" loops ?

The second thing I would like to do is to aggregate the first
data.frame by concatenating the 'type' values for each 'id', ie I'd
like to obtain something like :

id     value
10002  7
10061  114
10065  7
10114  114
10136  722

I have tried the "aggregate" function, but it doesn't work because the
"paste" function doesn't return a scalar value. Using tapply seems to
work, but is not straightforward, and I wanted to know if there is a
simple way to do this.

Thanks in advance for any help.

-- 
Julien



More information about the R-help mailing list