**2657 Productions News » R**, and kindly contributed to R-bloggers)

A while ago, there was a question on Stackoverflow, href="http://stackoverflow.com/questions/12544888/is-there-an-equivalent-r-function-to-stata-order-command">Is there an equivalent R function to Stata ‘order’ command?. There isn’t really, and for the most part, you don’t really need one, but I decided that, for fun, I would write one anyway.

id="more-1277">

Instead of operating directly on the `data.frame`

s, I decided to just work on an input vector, which can be, for example, `names(mydf)`

. This makes the function more flexible—the output can be used with `setcolorder()`

from the “data.table” package, for instance, for more memory efficient shuffling of columns.

Here’s the function:

```
moveme <- function(invec, movecommand) {
movecommand <- lapply(strsplit(strsplit(movecommand, ";")[[1]], ",|\\s+"),
function(x) x[x != ""])
movelist <- lapply(movecommand, function(x) {
Where <- x[which(x %in% c("before", "after", "first", "last")):length(x)]
ToMove <- setdiff(x, Where)
list(ToMove, Where)
})
myVec <- invec
for (i in seq_along(movelist)) {
temp <- setdiff(myVec, movelist[[i]][[1]])
A <- movelist[[i]][[2]][1]
if (A %in% c("before", "after")) {
ba <- movelist[[i]][[2]][2]
if (A == "before") {
after <- match(ba, temp)-1
} else if (A == "after") {
after <- match(ba, temp)
}
} else if (A == "first") {
after <- 0
} else if (A == "last") {
after <- length(myVec)
}
myVec <- append(temp, values = movelist[[i]][[1]], after = after)
}
myVec
}
```

Usage is simple:

```
myvec <- letters[1:10]
moveme(myvec, "a last; b, e, g before d; c first; h after j")
# [1] "c" "b" "e" "g" "d" "f" "i" "j" "h" "a"
```

Thus, assuming that you wanted to reorder the columns of a `data.frame`

named “mydf”, you would use:

```
mydf[moveme(names(mydf), "your move command")]
```

Similarly, if you wanted to reorder the columns of a `data.table`

named “DT”, you would use:

```
setcolorder(DT, names(mydf), moveme(names(mydf), "your move command"))
```

All in all, it was pretty fun to implement the function in a “natural language” way. Looking at the
href="http://www.stata.com/help.cgi?order">`order`

command from Stata, there are a couple of features that I have not addressed in this function. Perhaps I’ll add them some other day.

For now, it is a part of the href="https://github.com/mrdwab/mrdwabmisc">“mrdwabmisc” package, until it finds a more appropriate home.

**leave a comment**for the author, please follow the link and comment on his blog:

**2657 Productions News » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...