r - sorting set of data.frame by paired-row? -
this question has answer here:
- how sort dataframe column(s)? 15 answers
i have 3 data.frame objects, did combine them , remove duplicated instances, seems order not enough, want sort them in natural order. how can make 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 follows:
out <- rbind.data.frame(foo, bar, bleh) out <- out[!duplicated(out),]
but out doesn't have right order, want sort them in correct order 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 desired output format? lot
we can use order
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
Comments
Post a Comment