Re: [R] Obtaining replicates numbers of a vector

2007-12-20 Thread Moshe Olshansky
You could do: > x <- c('A','B','A','C','C','B') > x [1] "A" "B" "A" "C" "C" "B" > mapply(function(i) sum(x[1:i] == x[i]),1:length(x)) [1] 1 1 2 1 2 2 > --- Eric Lecoutre <[EMAIL PROTECTED]> wrote: > Dear R-help, > > I am trying to have a generic way to assess the > replicates in a character >

Re: [R] Obtaining replicates numbers of a vector

2007-12-19 Thread jim holtman
Here is another way of doing it: > x <- c('A','B','A','C','C','B') > ave(rep(1, length(x)), x, FUN=cumsum) [1] 1 1 2 1 2 2 > On Dec 19, 2007 5:36 AM, Eric Lecoutre <[EMAIL PROTECTED]> wrote: > Dear R-help, > > I am trying to have a generic way to assess the replicates in a character > vector. >

[R] Obtaining replicates numbers of a vector

2007-12-19 Thread Eric Lecoutre
Dear R-help, I am trying to have a generic way to assess the replicates in a character vector. Say that I have the following vector: x <- c('A','B','A','C','C','B') I would like to obtain: replicates <- c(1,1,2,1,2,2) each number beeing the time we see the corresponding value in x. Any clever

Re: [R] Obtaining replicates numbers of a vector

2007-12-19 Thread Eric Lecoutre
Thank you very much! This does indeed what I am looking for and really have R-ish look and feel. I just have turned that into a little handy function 'replicates' Best wishes, Eric 2007/12/19, Henrique Dallazuanna <[EMAIL PROTECTED]>: > > Try this: > > replicate <- vector("numeric", len=lengt

Re: [R] Obtaining replicates numbers of a vector

2007-12-19 Thread Henrique Dallazuanna
Try this: replicate <- vector("numeric", len=length(x)) replicate[order(x)] <- unlist(sapply(rle(sort(x))$lengths, seq_len)) On 19/12/2007, Eric Lecoutre <[EMAIL PROTECTED]> wrote: > Dear R-help, > > I am trying to have a generic way to assess the replicates in a character > vector. > Say that I