order a dataframe by a column that matches a vector in r

main.r
# Sample dataframe
df <- data.frame(id = c(1, 2, 3, 4, 5),
                 value = c(20, 10, 30, 15, 25))

# Vector to match
vec <- c(2, 5, 3, 1, 4)

# Ordering the dataframe by the 'value' column based on the vector order
df[match(vec, df$id), ]
248 chars
10 lines

related categories

gistlibby LogSnag