To order rows of a data frame according to a vector with a specific order, use the match() function in R.
The match() function returns a vector of the position of the first occurrence of the first vector in the second vector. If the element of the first vector does not exist in the second vector, NA is returned.
The basic syntax is given below:
match(x, table, nomatch = NA_integer_, incomparables = NULL)
Where,
x
vector or NULL: the values to be matched.
table
vector or NULL: the values to be matched against.
nomatch
the value to be returned in the case when no match is found. Note that it is coerced to integer.
incomparables
a vector of values that cannot be matched. Any value in x matching value in this vector is assigned the nomatch value. For historical reasons, FALSE is equivalent to NULL.
In your case:
df <- data.frame(name=letters[1:4], value=c(rep(TRUE, 2), rep(FALSE, 2))) target <- c("b", "c", "a", "d") df[match(target, df$name),]
Output:
name value 2 b TRUE 3 c FALSE 1 a TRUE 4 d FALSE