Мы можем преобразовать столбец 'fruits' в factor
с levels
, указанным в order
fruitDF[order(factor(fruitDF$fruits, levels = cat_order)),]
# fruits price
#2 apple 2.2
#1 banana 1.5
#3 orange 1.7
. Или другой вариант: match
fruitDF[match(fruitDF$fruits, cat_order),]
*. 1011 * Или с
data.table
library(data.table)
setDT(fruitDF)[order(match(fruits, order))]
data
fruitDF <- structure(list(fruits = c("banana", "apple", "orange"), price = c(1.5,
2.2, 1.7)), class = "data.frame", row.names = c(NA, -3L))