简体   繁体   English

如何根据另一个类别对因子水平进行排序?

[英]How to sort factor levels based on another category?

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: 并且levels(my.df$name)的输出是:

[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? 假设name级别始终与数据中category的同一级别相对应,我如何相应地对name级别进行排序?

Is this what you want? 这是你想要的吗?

> levels(my.df$name) <- as.character(unique(my.df[order(my.df$category),]$name))
> levels(my.df$name)
[1] "b" "c" "e" "f" "a" "d" "g" "h"

I think this might be cleaner than either of the solutions so far: 我认为这可能比到目前为止的任何一种解决方案都更清洁:

    my.df <-
structure(list(name = structure(1:8, .Label = c("a", "b", "c", 
"d", "e", "f", "g", "h"), class = "factor"), category = structure(c(1L, 
1L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("A", "B"), class = "factor")), .Names = c("name", 
"category"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8"))

 with(my.df, name[order(category)] )
[1] b d e h a c f g
Levels: a b c d e f g h

If you wanted to relevel the factor, that could be done as well, but it wasn't clear that you wanted that change to be permanent. 如果您想重新调整因子,也可以这样做,但是尚不清楚您是否希望这种改变是永久的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM