[R] Recode numbers
William Dunlap
wdunlap at tibco.com
Wed Jun 1 22:21:37 CEST 2011
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Lisa
> Sent: Wednesday, June 01, 2011 12:28 PM
> To: r-help at r-project.org
> Subject: Re: [R] Recode numbers
>
> Thank you, Duncan,
>
> Here "a" has the length of 24, and "b" has the length of 20
> with numbers
> from 1 to 20 uniquely. I just want encode "a" from 1 to 20
> based on "a"
> current order using "b". So, a1[1] = b[1] = 1
> a1[2] = b[2] = 5
> a1[3] = a1[4] = b[3] = 8 (since third and fourth numbers are
> the same in
> "a")
> a1[5] = a1[6] = b[4] = 9
> a1[7] = b[5] = 14
> a1[8] = b[6] = 20
> a1[9] = b[7] = 3
> a1[10] = a1[11] = b[8] = 10
> ...
> a1[23] = b[19] = 15
> a1[24] = b[20] = 19
I don't understand the verbal description, but the the
following makes your example work:
> b[cumsum(c(TRUE,a[-1]!=a[-length(a)]))]
[1] 1 5 8 8 9 9 14 20 3 10 10 12 6 16 7 11 13 13
[19] 17 18 2 4 15 19
> identical(.Last.value, a1)
[1] TRUE
Is that what you want? The cumsum(...) increments each
at each changepoint in a.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566681.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list