Say I have a data frame with two factors in there and I want to sort the levels of one factor grouped by the second category.
name <- letters[1:8]
category <- factor(sample(1:2, 8, replace=T), labels=c("A", "B"))
my.df <- data.frame(name=name, category=category)
So the data frame looks similar to:
name category
1 a A
2 b A
3 c B
4 d B
5 e B
6 f A
7 g A
8 h A
and the output of levels(my.df$name) is:
[1] "a" "b" "c" "d" "e" "f" "g" "h"
Assuming that a level in name always corresponds to the same level in category in my data, how can I sort the levels of name accordingly?
Is this what you want?