[R] 3 questions: debug R script, multi-level sorts, and multi-gsub

David Winsemius dwinsemius at comcast.net
Sat Apr 26 14:37:25 CEST 2008


"Ng Stanley" <stanleyngkl at gmail.com> wrote in
news:462b7fdd0804252028w4901ae25v11d53161207d139c at mail.gmail.com: 

> 2) I have looked deeply into R, but can't fine multi-level sorts.

At the top of the help page for sort...

"Sort (or order) a vector or factor (partially) into ascending (or 
descending) order. For ordering along more than one variable, e.g., for 
sorting data frames, see _order_. "

In the examples on the order page, it shows what I suspect you mean by 
a multilevel sort (although you offer no examples).

> (ii <- order(x <- c(1,1,3:1,1:4,3), y <- c(9,9:1), z <-c(2,1:9)))
 [1]  6  5  2  1  7  4 10  8  3  9
> dd <- transform(data.frame(x,y,z),
+                 z = factor(z, labels=LETTERS[9:1]))
> dd
   x y z
1  1 9 H
2  1 9 I
3  3 8 H
4  2 7 G
5  1 6 F
6  1 5 E
7  2 4 D
8  3 3 C
9  4 2 B
10 3 1 A

> dd[ order(x, -y, z) ,]
   x y z
2  1 9 I
1  1 9 H
5  1 6 F
6  1 5 E
4  2 7 G
7  2 4 D
3  3 8 H
8  3 3 C
10 3 1 A
9  4 2 B

-- 
David Winsemius


> 
> 3) I have a vector of words, several of which need to be replaced by
> different unique words. I am using multi-lines of gsub, is there any
> elegant alternative ?
> 
>      [[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. 
>



More information about the R-help mailing list