Hello, I have the following data1 (index are chars): ?? ?index 1 ?008823 2 ?012689 3 ?004503 4 ?002991 5 ?012689 6 ?002845 7 ?012689 8 ?012395 9 ?012689 10 009302 11 002845 12 006669 13 008823 14 009302 15 025340 16 012689 and data2 in this format (index2 are chars): ?? ? ? index2 ? ? ?tic 1 ? ? 001003 ? ? ANTQ 2 ? ? 001004 ? ? AIR 3 ? ? 001009 ? ? ABSI 4 ? ? 001011 ? ? ACSE etc I am attempting to merge them using the following: mergeddata <- merge(data1,data2, by.x="index", by.y="index2", sort=FALSE) I want to preserve the order of data1 by setting sort=FALSE, however the output I get is: ?? index ? tic 1 ?008823 PHM 2 ?008823 PHM 3 ?012689 KBH 4 ?012689 KBH 5 ?012689 KBH 6 ?012689 KBH 7 ?012689 KBH 8 ?004503 XOM 9 ?002991 CVX 10 002845 CTX 11 002845 CTX 12 012395 TOL 13 009302 RYL 14 009302 RYL 15 006669 LEN 16 025340 DHI which has not preserved the order of data1, and also does not have a clear relationship to the order of data2 that I can see. I also tried converting both index fields from chars to ints and got the same result. Any ideas of what is going on here?
Why yes. If you keep reading the helpfile for merge, you come to this bit: Value: A data frame. The rows are by default lexicographically sorted on the common columns, but for ?sort = FALSE? are in an unspecified order. sort=FALSE doesn't preserve your order; instead it gives you an unspecified potentially random order. Sarah On Wed, Jan 27, 2010 at 5:13 PM, lol zino <lolzino at gmail.com> wrote:> Hello, >[...]> > I am attempting to merge them using the following: > > mergeddata <- merge(data1,data2, by.x="index", by.y="index2", sort=FALSE) > > I want to preserve the order of data1 by setting sort=FALSE, however > the output I get is: >[...]> > which has not preserved the order of data1, and also does not have a > clear relationship to the order of data2 that I can see. I also tried > converting both index fields from chars to ints and got the same > result. Any ideas of what is going on here? > > ______-- Sarah Goslee http://www.functionaldiversity.org
You could add an extra sequence on the dataframe you wish to sort on. Merge together, sort by the sequence, delete the sequence. It's a bit more work, but it will give you what you want. Bart -- View this message in context: http://n4.nabble.com/Merge-sort-F-not-preserving-order-tp1312234p1340790.html Sent from the R help mailing list archive at Nabble.com.