2

I have a data frame with over 1000 rows.

1   chr1    23373262    23390014    NM_001081079    -   Ogfrl1
10  chr1    43807075    43884553    NM_026430   -   Uxs1
100 chr10   77069342    77081076    NM_001301671    +   Sumo3
1000    chr5    137736931   137748902   NM_031405   -   Srrt
1001    chr5    137737851   137737916   NR_106003   -    
1002    chr5    137751126   137755469   NM_011639   -   Trip6
1003    chr5    137755785   137774810   NM_031406   -   Slc12a9
1004    chr5    138220145   138221335   NM_027242   +   Ppp1r35
1005    chr5    138223133   138227929   NM_144913   -   Mepce
1006    chr5    138229029   138263849   NM_001005426    +   Zcwpw1

I want to sort them by row.names, which the the number in the first column above. My command is

mef_genes<-mef_genes[sort(as.integer(row.names(mef_genes)),decreasing=F),]

Its ranked as shown above. I want to rank like normally ascending, i.e 1,2,3... Can anyone tell me how to do it? Thank you very much

lxcfuji
  • 329
  • 1
  • 4
  • 15
  • Your output is sorted correctly. Did you remove any rows of the original data? – martin Apr 03 '15 at 18:02
  • Please provide [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), for example by providing your data via: `dput(mef_genes)`. – Thomas Apr 03 '15 at 18:09
  • @Martin Why do you say it's sorted correctly? – Dominic Comtois Apr 03 '15 at 18:13
  • @DominicComtois he says that because of the rows shown none are out of order. You have to assume there's a row `2` that comes much later to see that it's incorrect. – Gregor Thomas Apr 03 '15 at 18:44
  • @Gregor ok I see... well it seems like a reasonable assumption, given the OP asks the question. – Dominic Comtois Apr 03 '15 at 18:50
  • @DominicComtois I agree completely, just trying to explain the possibility of confusion---though I do have to say when I clicked on the question, based on the title I was hoping OP wanted help creating an incorrect sorting. The difference between `order` and `sort` is a boring old R-Faq. – Gregor Thomas Apr 03 '15 at 18:53
  • possible duplicate of [How to sort a dataframe by column(s) in R](http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r) – Gregor Thomas Apr 03 '15 at 18:55
  • @Gregor haha yes, in fact it probably should be reworded to something like "incorrectly sorted row numbers". – Dominic Comtois Apr 03 '15 at 20:58

1 Answers1

3

Try order instead of sort:

mef_genes <- mef_genes[order(as.integer(row.names(mef_genes))),]
Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61