[R] Ordering numbers

Greg Snow Greg.Snow at imail.org
Wed Dec 2 21:42:54 CET 2009


Here is one way:

> id <- c(1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 1, 2, 2, 2, 3, 3, 1, 1, 1, 2, 3, 4,
+ 4, 4, 5, 5)
> id
 [1] 1 1 2 2 2 3 4 4 4 4 1 2 2 2 3 3 1 1 1 2 3 4 4 4 5 5
> tmp <- rle(id)
> tmp
Run Length Encoding
  lengths: int [1:12] 2 3 1 4 1 3 2 3 1 1 ...
  values : num [1:12] 1 2 3 4 1 2 3 1 2 3 ...
> rep( seq_along(tmp$lengths), tmp$lengths )
 [1]  1  1  2  2  2  3  4  4  4  4  5  6  6  6  7  7  8  8  8  9 10 11 11 11 12
[26] 12

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Lisa
> Sent: Wednesday, December 02, 2009 1:30 PM
> To: r-help at r-project.org
> Subject: [R] Ordering numbers
> 
> 
> Hello all,
> 
> I have a set of numbers that looks like this:
> 
> > id <- c(1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 1, 2, 2, 2, 3, 3, 1, 1, 1, 2,
> 3, 4,
> > 4, 4, 5, 5)
> > id
>  [1] 1 1 2 2 2 3 4 4 4 4 1 2 2 2 3 3 1 1 1 2 3 4 4 4 5 5
> 
> Please ignore the bold numbers, I just want to make my problem clear.
> I am going to order them as this:
> 
> 1 1 2 2 2 3 4 4 4 4 5 6 6 6 7 7 8 8 8 9 10 11 11 11 12 12
> 
> Can anyone please help how to get this done? Your help would be greatly
> appreciated.
> 
> Lisa
> 
> --
> View this message in context: http://n4.nabble.com/Ordering-numbers-
> tp941453p941453.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