1

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?

Community
  • 1
  • 1
boshek
  • 4,100
  • 1
  • 31
  • 55

1 Answers1

8

The answer here, actually provided by @RichScriven is that I wasn't setting the order of the column I wanted (df$ftor) and rather the whole data.frame (df). In addition, the indexing was wonky. So ultimately I replaced this:

df$ftor <- factor(df$ftor, levels=df[order(df$order_ID),], ordered=TRUE)

with this:

df$ftor <- factor(df$ftor, levels=df$ftor[order(df$order_ID)], ordered=TRUE)
boshek
  • 4,100
  • 1
  • 31
  • 55