Ignacio
Ignacio

Reputation: 7938

Reorder factor variable by mean of variable within that factor

Suppose my data looks something like this:

set.seed(782015)
df1 <-
  data.frame(name = factor(sample(
    x = LETTERS[1:4], size = 1000, prob = c(.25,.25,.25,.25), replace = T
  )),
  number = rnorm(n = 1000, mean = 100, sd = 10))

I want to reorder the factor variable name by having the one with the highest mean(number) first and the lowers last. In this example it should be:

  name        
1    B 
2    C 
3    D 
4    A 

What is the best way of doing this?

Thanks!


To clarify:

 levels(df1$name)
 [1] "A" "B" "C" "D"

I want it to return B C D A

Upvotes: 2

Views: 88

Answers (2)

Aaron - mostly inactive
Aaron - mostly inactive

Reputation: 37804

You want (wait for it...) reorder.

> df1$name <- reorder(df1$name, -df1$number)
> levels(df1$name)
[1] "B" "C" "D" "A"

Upvotes: 6

HasaniH
HasaniH

Reputation: 8402

You can try this:

ag <- aggregate(number ~ name, df1, FUN=mean)
levels(df1$name) <- ag[order(ag$number, decreasing=T),]$name

Upvotes: 0

Related Questions