Is there an easier way to ensure that a data frame's rows are ordered according to a "target" vector as the one I implemented in the short example below?
df <- data.frame(name = letters[1:4], value = c(rep(TRUE, 2), rep(FALSE, 2)))
df
# name value
# 1 a TRUE
# 2 b TRUE
# 3 c FALSE
# 4 d FALSE
target <- c("b", "c", "a", "d")
This somehow seems to be a bit too "complicated" to get the job done:
idx <- sapply(target, function(x) {
which(df$name == x)
})
df <- df[idx,]
rownames(df) <- NULL
df
# name value
# 1 b TRUE
# 2 c FALSE
# 3 a TRUE
# 4 d FALSE
I prefer to use
***_join
indplyr
whenever I need to match data. One possible try for thisNote that the input for
***_join
require tbls or data.frameTry
match
:It will work as long as your
target
contains exactly the same elements asdf$name
, and neither contain duplicate values.From
?match
:Therefore
match
finds the row numbers that matchestarget
's elements, and then we returndf
in that order.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 thegdata
package.Next, use the fact that it is now ordered:
If you want to go back to the original (alphabetic) ordering, just use
as.character()
to get it back to the original state.