In order to properly extol the virtues of setorder and understand the price we pay for it, we need to discuss what it means to modify a structure by reference as opposed to by value. This is a somewhat advanced topic but I feel that it is essential for (a) learning how data.table is so powerful and fast, and (b) preventing you, dear reader, from corrupting your data accidentally. In addition, the material in this discussion introduces concepts and terms in computer science that will serve you well to know for programming in other languages.
Most R programs use expressions that use by value semantics.
Here’s an example code.
Let’s make a two copies of mtcars, called mtcopy and mtcopy2, using the ...