Hello, I have a dataframe: dd <- data.frame(b = c("chr2", "chr1", "chr15", "chr13"), x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), z = c(1, 1, 1, 2))>ddb x y z 1 chr2 A 8 1 2 chr1 D 3 1 3 chr15 A 9 1 4 chr13 C 9 2 Now I want to sort them according column "b", but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]]
> ddb x y z 1 chr2 A 8 1 2 chr1 D 3 1 3 chr15 A 9 1 4 chr13 C 9 2> # add column with just numbers > dd$sort <- as.integer(gsub("\\D+", "", dd$b)) > dd[order(dd$sort),] # notice it is a numeric, not character orderb x y z sort 2 chr1 D 3 1 1 1 chr2 A 8 1 2 4 chr13 C 9 2 13 3 chr15 A 9 1 15>On Thu, May 20, 2010 at 8:28 AM, Yuan Jian <jayuan2008 at yahoo.com> wrote:> Hello, > > I have a dataframe: > dd <- data.frame(b = c("chr2", "chr1", "chr15", "chr13"), > ????? x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), > ?????? z = c(1, 1, 1, 2)) > >>dd > ????? b x y z > 1? chr2 A 8 1 > 2? chr1 D 3 1 > 3 chr15 A 9 1 > 4 chr13 C 9 2 > > Now I want to sort them according column "b", but only its number is considered: > ????? b x y z > 1? chr1 D 3 1 > 2?chr13 C 9 2 > 3?chr15 A 9 1 > 4? chr2 A 8 1 > > thanks > jian > > > > > > > ? ? ? ?[[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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Try this. dd[order(gsub("chr","",dd$b)),] You need regular expressions if chr is not the only characterstring that is prepended to the numbers. look for ?strsplit Nikhil Kaza University of North Carolina nikhil.list at gmail.com On May 20, 2010, at 8:28 AM, Yuan Jian wrote:> Hello, > > I have a dataframe: > dd <- data.frame(b = c("chr2", "chr1", "chr15", "chr13"), > x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), > z = c(1, 1, 1, 2)) > >> dd > b x y z > 1 chr2 A 8 1 > 2 chr1 D 3 1 > 3 chr15 A 9 1 > 4 chr13 C 9 2 > > Now I want to sort them according column "b", but only its number is > considered: > b x y z > 1 chr1 D 3 1 > 2 chr13 C 9 2 > 3 chr15 A 9 1 > 4 chr2 A 8 1 > > thanks > jian > > > > > > > [[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.
The solution given by Jim is more correct dd[order(as.numeric(substr(dd$b,4,5))),] regards M jim holtman a ?crit :>> dd >> > b x y z > 1 chr2 A 8 1 > 2 chr1 D 3 1 > 3 chr15 A 9 1 > 4 chr13 C 9 2 > >> # add column with just numbers >> dd$sort <- as.integer(gsub("\\D+", "", dd$b)) >> dd[order(dd$sort),] # notice it is a numeric, not character order >> > b x y z sort > 2 chr1 D 3 1 1 > 1 chr2 A 8 1 2 > 4 chr13 C 9 2 13 > 3 chr15 A 9 1 15 > > > > On Thu, May 20, 2010 at 8:28 AM, Yuan Jian <jayuan2008 at yahoo.com> wrote: > >> Hello, >> >> I have a dataframe: >> dd <- data.frame(b = c("chr2", "chr1", "chr15", "chr13"), >> x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), >> z = c(1, 1, 1, 2)) >> >> >>> dd >>> >> b x y z >> 1 chr2 A 8 1 >> 2 chr1 D 3 1 >> 3 chr15 A 9 1 >> 4 chr13 C 9 2 >> >> Now I want to sort them according column "b", but only its number is considered: >> b x y z >> 1 chr1 D 3 1 >> 2 chr13 C 9 2 >> 3 chr15 A 9 1 >> 4 chr2 A 8 1 >> >> thanks >> jian >> >> >> >> >> >> >> [[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. >> >> >> > > > >
Hi Yuan, Try dd[order(as.numeric(gsub("[^0-9]", "", dd$b))), ] HTH, Jorge On Thu, May 20, 2010 at 8:28 AM, Yuan Jian <> wrote:> Hello, > > I have a dataframe: > dd <- data.frame(b = c("chr2", "chr1", "chr15", "chr13"), > x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), > z = c(1, 1, 1, 2)) > > >dd > b x y z > 1 chr2 A 8 1 > 2 chr1 D 3 1 > 3 chr15 A 9 1 > 4 chr13 C 9 2 > > Now I want to sort them according column "b", but only its number is > considered: > b x y z > 1 chr1 D 3 1 > 2 chr13 C 9 2 > 3 chr15 A 9 1 > 4 chr2 A 8 1 > > thanks > jian > > > > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]
If you want to sort the data frame according to column "b", the followding code does this work. attach(dd) dd<-dd[order(b),] detach(dd) If you want to sort the data frame according to the chr number in column b, you should extract the numbers first into a vector, say chrnum, and then use order(chrnum) as the index to sort the data frame dd. Suppose that the chr numbers have been extracted to vector chrnum. The code is as follows: chrnum <-c(2,1,15,13) attach(dd) dd<-dd[order(chrnum),] detach(dd) The result is > dd b x y z 2 chr1 D 3 1 1 chr2 A 8 1 4 chr13 C 9 2 3 chr15 A 9 1 But I don't know how to extract the numbers conveniently. Please respond if anyone knows. Many thanks! On 05/20/2010 08:28 PM, Yuan Jian wrote:> Hello, > > I have a dataframe: > dd<- data.frame(b = c("chr2", "chr1", "chr15", "chr13"), > x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), > z = c(1, 1, 1, 2)) > > >> dd >> > b x y z > 1 chr2 A 8 1 > 2 chr1 D 3 1 > 3 chr15 A 9 1 > 4 chr13 C 9 2 > > Now I want to sort them according column "b", but only its number is considered: > b x y z > 1 chr1 D 3 1 > 2 chr13 C 9 2 > 3 chr15 A 9 1 > 4 chr2 A 8 1 > > thanks > jian > > >