An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20071219/bc123728/attachment.pl
Obtaining replicates numbers of a vector
5 messages · Eric Lecoutre, Henrique Dallazuanna, jim holtman +1 more
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 <ericlecoutre at gmail.com> wrote:
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 and generic way to obtain that?
Eric
--
Eric Lecoutre
Consultant - Business & Decision
Business Intelligence & Customer Intelligence
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20071219/ff03e8d8/attachment.pl
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 <ericlecoutre at gmail.com> wrote:
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 and generic way to obtain that?
Eric
--
Eric Lecoutre
Consultant - Business & Decision
Business Intelligence & Customer Intelligence
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
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 <ericlecoutre at gmail.com> wrote:
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 and generic way to obtain that?
Eric
--
Eric Lecoutre
Consultant - Business & Decision
Business Intelligence & Customer Intelligence
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.