# Why is merge sorting even when sort = F?

7 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Why is merge sorting even when sort = F?

 Hello! I have a vector 'grades' and a data frame 'info': grades2 <- data.frame(grade = c(1,2,2,3,1)) info <- data.frame(   grade = 3:1,   desc = c("Excellent", "Good", "Poor"),   fail = c(F, F, T) ) I want to get the info for all grades I have in info: This solution resorts everything in the order of column 'grade': merge(grades2, info, by = "grade", all.x = T, all.y = F) Could you please explain why this solution also resorts - despite sort = FALSE? merge(grades2, info, by = "grade", all.x = T, all.y = F, sort = FALSE) Thanks a lot! -- Dimitri Liakhovitski ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Why is merge sorting even when sort = F?

 Merging is not necessarily an order-preserving operation, but sorting can make the operation more efficient. The sort=TRUE argument forces the result to be sorted, but sort=FALSE is in not a promise that order will be preserved. (I think the imperfect sorting occurs when there are multiple keys but am not sure.) You can add columns to the input data that let you restore some semblance of the original ordering afterward, or you can roll your own possibly-less-efficient merge using match and indexing: info[ match( grades2\$grade, info\$grade ), ] -- Sent from my phone. Please excuse my brevity. On March 8, 2017 8:07:27 AM PST, Dimitri Liakhovitski <[hidden email]> wrote: >Hello! >I have a vector 'grades' and a data frame 'info': > >grades2 <- data.frame(grade = c(1,2,2,3,1)) >info <- data.frame( >  grade = 3:1, >  desc = c("Excellent", "Good", "Poor"), >  fail = c(F, F, T) >) > >I want to get the info for all grades I have in info: > >This solution resorts everything in the order of column 'grade': >merge(grades2, info, by = "grade", all.x = T, all.y = F) > >Could you please explain why this solution also resorts - despite sort >= FALSE? >merge(grades2, info, by = "grade", all.x = T, all.y = F, sort = FALSE) > >Thanks a lot! ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Why is merge sorting even when sort = F?

 Thank you. I was just curious what sort=FALSE had no impact. Wondering what it is there for then... On Wed, Mar 8, 2017 at 11:43 AM, Jeff Newmiller <[hidden email]> wrote: > Merging is not necessarily an order-preserving operation, but sorting can make the operation more efficient. The sort=TRUE argument forces the result to be sorted, but sort=FALSE is in not a promise that order will be preserved. (I think the imperfect sorting occurs when there are multiple keys but am not sure.) You can add columns to the input data that let you restore some semblance of the original ordering afterward, or you can roll your own possibly-less-efficient merge using match and indexing: > > info[ match( grades2\$grade, info\$grade ), ] > -- > Sent from my phone. Please excuse my brevity. > > On March 8, 2017 8:07:27 AM PST, Dimitri Liakhovitski <[hidden email]> wrote: >>Hello! >>I have a vector 'grades' and a data frame 'info': >> >>grades2 <- data.frame(grade = c(1,2,2,3,1)) >>info <- data.frame( >>  grade = 3:1, >>  desc = c("Excellent", "Good", "Poor"), >>  fail = c(F, F, T) >>) >> >>I want to get the info for all grades I have in info: >> >>This solution resorts everything in the order of column 'grade': >>merge(grades2, info, by = "grade", all.x = T, all.y = F) >> >>Could you please explain why this solution also resorts - despite sort >>= FALSE? >>merge(grades2, info, by = "grade", all.x = T, all.y = F, sort = FALSE) >> >>Thanks a lot! -- Dimitri Liakhovitski ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Why is merge sorting even when sort = F?

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Why is merge sorting even when sort = F?

Open this post in threaded view
|
Report Content as Inappropriate