Is there any way available to ensure that a data frame's rows are ordered according to the required/target vector.
Consider the example below for reference?
data <- data.frame(alphabets = letters[1:4], value = c(rep(TRUE, 2), rep(FALSE, 2)))
data
# alphabets value
# 1 a TRUE
# 2 b TRUE
# 3 c FALSE
# 4 d FALSE
target <- c("b", "c", "a", "d")
However, the method below seems to be a little bit complicated:
id <- sapply(target, function(x) {
which(data$alphabets == x)
})
data <- data[id,]
rownames(data) <- NULL
data
# alphabets value
# 1 b TRUE
# 2 c FALSE
# 3 a TRUE
# 4 d FALSE