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

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sat, 26 Apr 2008 12:37:25 +0000 (UTC)

"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.
> ______________________________________________ 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.
Received on Sat 26 Apr 2008 - 12:41:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 26 Apr 2008 - 13:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive