1

I have three data.frame objects, where I did combine them and also remove duplicated instances, but seems order is not good enough, I want to sort them in natural order. How can make this happen more efficiently?

toy data

foo <- data.frame( start=seq(1, by=4, len=6), stop=seq(3, by=4, len=6))
bar <- data.frame(start=seq(5, by=2, len=7), stop=seq(7, by=2, len=7))
bleh <- data.frame(start=seq(1, by=5, len=5), stop=seq(3, by=5, len=5))

I did combine them as follows:

out <- rbind.data.frame(foo, bar, bleh)
out <- out[!duplicated(out),]

but out doesn't have right order, I want to sort them in correct order by row.

desired output: (manually pin out)

       start  stop
1      1      3
2      5      7
3      6      8
4      7      9
5      9      11
6      11     13
7      13     15
8      15     17
9      16     18
10     17     19
11     21     23

how can I get my desired output format? Thanks a lot

Community
  • 1
  • 1
  • I checked this post, but it is about sort them by column, I am interested in sorting them by row instead. I hope this is clearly motivated. –  Jul 28 '16 at 09:16

1 Answers1

2

We can use order with do.call

out1 <- out[do.call(order, out),]
out1
#    start stop
#1      1    3
#2      5    7
#15     6    8
#8      7    9
#3      9   11
#10    11   13
#4     13   15
#12    15   17
#17    16   18
#5     17   19
#6     21   23

rownames(out1) <- NULL
akrun
  • 874,273
  • 37
  • 540
  • 662
  • indeed, row are reordered in desired way, but seems corresponding index did not change. Any way reorder index of each row accordingly? Thx –  Jul 28 '16 at 09:23
  • 1
    @datageek We can assign the rownames to NULL – akrun Jul 28 '16 at 09:24