I been trying to figure out how to order a factor based on the value of another column all in the same data.frame. I have been using this question as the basis for my efforts:
R - Order a factor based on value in one or more other columns
In my example I like the levels of ftor
set by order_ID
. order_ID
is simply a rank of the order. Here is what I have so far:
df <- data.frame(order_ID=c(5,3,1,4,2),
ftor=LETTERS[1:5])
df$ftor <- factor(df$ftor, levels=df[order(df$order_ID),], ordered=TRUE)
This is what I get when I try levels after the above attempt:
> levels(df$ftor)
[1] "c(1, 2, 3, 4, 5)" "c(3, 5, 2, 4, 1)"
Based on the value of order_ID
here is what I am expecting:
> levels(df$ftor)
[1] "C" "B" "E" "D" "A"
In summary, can anyone recommend an approach where I can use the levels of one column to set the levels of another factor in the same data.frame?