Hi, I have a character vector as below: a<-c('10','3R','4','4R','5','5R','6','6R','7','8','9','7R','1','10R','11' ,'11R','12','12R','13','13R','14','14R','15','15R','1R','2','2R','3','8R' ,'9R') Is there a clever way to sort this easily to return a vector of index that would produce a vector as below: a[index]: [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" [11] "11" "12" "13" "14" "15" "1R" "2R" "3R" "4R" "5R" [21] "6R" "7R" "8R" "9R" "10R" "11R" "12R" "13R" "14R" "15R" Thanks, John [[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of array chip > Sent: Monday, March 14, 2011 10:33 AM > To: r-help at r-project.org > Subject: [R] ideas on sorting > > Hi, I have a character vector as below: > > a<-c('10','3R','4','4R','5','5R','6','6R','7','8','9','7R','1' > ,'10R','11' > ,'11R','12','12R','13','13R','14','14R','15','15R','1R','2','2 > R','3','8R' > ,'9R') > > Is there a clever way to sort this easily to return a vector > of index that would > produce a vector as below: > > a[index]: > > [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" > [11] "11" "12" "13" "14" "15" "1R" "2R" "3R" "4R" "5R" > [21] "6R" "7R" "8R" "9R" "10R" "11R" "12R" "13R" "14R" "15R"Break up the strings into an initial number (as a number, not a string, for they sort differently) and a trailing string starting with the first non-digit. Order them by the trailing string, breaking ties with the initial number: > index <- order(gsub("^[[:digit:]]+", "", a), # trailing string as.integer(gsub("([[:digit:]]+).*$", "\\1", a))) # break ties with initial numbers > a[index] [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" [13] "13" "14" "15" "1R" "2R" "3R" "4R" "5R" "6R" "7R" "8R" "9R" [25] "10R" "11R" "12R" "13R" "14R" "15R" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > Thanks, > > John > > > > [[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. >
array chip wrote:> > Hi, I have a character vector as below: > > a<-c('10','3R','4','4R','5','5R','6','6R','7','8','9','7R','1','10R','11' > ,'11R','12','12R','13','13R','14','14R','15','15R','1R','2','2R','3','8R' > ,'9R') > > Is there a clever way to sort this easily to return a vector of index that > would > produce a vector as below: > > a[index]: > > [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" > [11] "11" "12" "13" "14" "15" "1R" "2R" "3R" "4R" "5R" > [21] "6R" "7R" "8R" "9R" "10R" "11R" "12R" "13R" "14R" "15R" >Try index <- order(as.numeric(sub('R','000',a))) /Berend -- View this message in context: http://r.789695.n4.nabble.com/ideas-on-sorting-tp3354489p3354525.html Sent from the R help mailing list archive at Nabble.com.
On 03/14/2011 10:32 AM, array chip wrote:> Hi, I have a character vector as below: > > a<-c('10','3R','4','4R','5','5R','6','6R','7','8','9','7R','1','10R','11' > ,'11R','12','12R','13','13R','14','14R','15','15R','1R','2','2R','3','8R' > ,'9R') > > Is there a clever way to sort this easily to return a vector of index that would > produce a vector as below: > > a[index]: > > [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" > [11] "11" "12" "13" "14" "15" "1R" "2R" "3R" "4R" "5R" > [21] "6R" "7R" "8R" "9R" "10R" "11R" "12R" "13R" "14R" "15R"treat these as levels of a factor and let R do the thinking lvls = c(1:15, paste(1:15, "R", sep="")) a = factor(c('10', '3R', '4', '4R', '5', '5R', '6', '6R', '7', '8', '9', '7R', '1', '10R', '11', '11R', '12', '12R', '13', '13R', '14', '14R', '15', '15R', '1R', '2', '2R', '3', '8R' , '9R'), levels=lvls) > sort(a) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [15] 15 1R 2R 3R 4R 5R 6R 7R 8R 9R 10R 11R 12R 13R [29] 14R 15R 30 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1R 2R ... 15R > a[order(a)] [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [15] 15 1R 2R 3R 4R 5R 6R 7R 8R 9R 10R 11R 12R 13R [29] 14R 15R 30 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1R 2R ... 15R Martin> > Thanks, > > John > > > > [[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.-- Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793