-1

I would like to order all lines based on two column values in R. This is my input:

chr   start   no

 4     85     non1
 4     23     non2
 6     10     non2
 8     25     non2
 22    56     non4
 2     15     non1

This is my expected output:

chr   start   no
 2     15     non1
 4     23     non2
 4     85     non1
 6     10     non2
 8     25     non2
 22    56     non4

Thank You. Cheers.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
user3091668
  • 2,230
  • 6
  • 25
  • 42
  • 2
    if your data is in a data frame, try http://stackoverflow.com/questions/6769703/order-data-frame-by-two-columns-in-r – user2510479 Feb 17 '14 at 18:36

2 Answers2

1

The order function accepts a variable number of input vectors, ordering by the first , then second and so on ...

BED=read.table(text=
"chr   start   no
4     85     non1
4     23     non2
6     10     non2
8     25     non2
22    56     non4
2     15     non1", header=T)

BED[order(BED$chr, BED$start),]
   chr start   no
6   2    15 non1
2   4    23 non2
1   4    85 non1
3   6    10 non2
4   8    25 non2
5  22    56 non4
Stephen Henderson
  • 6,340
  • 3
  • 27
  • 33
  • Thank you! But in my all data I have chr=10 and chr=1, and these function orders 1 and after 10. (the right ordering have to be 1 and 2 and 3...) – user3091668 Feb 17 '14 at 19:02
  • ah yes a common problem. I take it you have an x and y chromosome, hence the column is character? I think I usually make it `as.factor` then `reorder(thefactor, therightorder)`. Irritating , I know. – Stephen Henderson Feb 17 '14 at 19:16
1

While you can certainly use order from the base package, for working with data frames I'd highly recommend using the plyr package.

chr <- c(4,4,6,8,22,2)
start <- c(85, 23, 10, 25, 56, 15)
no <- c("non1", "non2", "non2", "non2", "non4", "non1")

myframe <- data.frame(chr, start, no)

creates your data frame. In terms of dealing with the character column:

myframe$chr <- as.numeric(myframe$chr)

and then getting the arranged version is very easy:

library(plyr)
arrangedFrame <- arrange(myframe, chr, start)
print(arrangedFrame)

  chr start   no
1   2    15 non1
2   4    23 non2
3   4    85 non1
4   6    10 non2
5   8    25 non2
6  22    56 non4

there are also a lot of easily modified options using arrange that make different reorderings easier than using order. And while I haven't used it a lot yet, I know Hadley released dplyr not too long ago which offers even more functionality and which I'd encourage you to check out.

TomR
  • 546
  • 8
  • 19
  • The function `arrange` works also with the excellent [dplyr] (http://cran.r-project.org/web/packages/dplyr/index.html) package. – user2030503 Feb 18 '14 at 05:31