This method is a bit different, it provided me with a bit more flexibility than the previous answer.
By making it into an ordered factor, you can use it nicely in arrange
and such. I used reorder.factor from the gdata
package.
df <- data.frame(name=letters[1:4], value=c(rep(TRUE, 2), rep(FALSE, 2)))
target <- c("b", "c", "a", "d")
require(gdata)
df$name <- reorder.factor(df$name, new.order=target)
Next, use the fact that it is now ordered:
require(dplyr)
df %>%
arrange(name)
name value
1 b TRUE
2 c FALSE
3 a TRUE
4 d FALSE
If you want to go back to the original (alphabetic) ordering, just use as.character()
to get it back to the original state.