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.