[英]How can I sort a dataframe by a predetermined order of factor levels in R?
I have a data frame in which one column consists of unique factors. 我有一个数据框,其中一列包含唯一因素。 I want to sort this data frame by a predefined order of factor levels, independend of the original order.
我想按照预定的因子水平顺序对数据框进行排序,而不必依赖原始顺序。
For example my data looks like this: 例如,我的数据如下所示:
label <- c('tree','lake','house', 'human')
number <- c(50,1,2,5)
df <- data.frame(
group = label,
value = number)
category_order <- category_order = c('tree','house','lake','human')
where df has the form df的形式
group number
1 tree 50
2 lake 1
3 house 2
4 human 5
but I would like it to be sorted in like category_oder so df_new looks like: 但我希望将其分类为category_oder,因此df_new如下所示:
group number
1 tree 50
2 house 2
3 lake 1
4 human 5
I know know that in this case I could just swap the second and third row, but in general I don't know in which order the facors will be in the data frame and I couldn't find a way to do this without having strong restrictions about what factors I can use and the order in which they shoud be in the end. 我知道在这种情况下,我可以交换第二行和第三行,但是总的来说,我不知道facors会以哪种顺序出现在数据帧中,如果没有强大的功能,我将找不到解决办法关于我可以使用哪些因素以及它们应遵循的顺序的限制。 (for example alphabetical order)
(例如,字母顺序)
We can specify the levels
of the 'group' as category_order
and that use that to `arrange 我们可以将“组”的
levels
指定为category_order
并使用它来“安排”
library(dplyr)
df1 <- df %>%
arrange(factor(group, levels = category_order))
df1
# group value
#1 tree 50
#2 house 2
#3 lake 1
#4 human 5
Or using fct_relevel
或使用
fct_relevel
library(forcats)
df %>%
arrange(fct_relevel(group, category_order))
In base R, we can use match
to get the order of group
based on category_order
在基数R中,我们可以使用
match
根据category_order
获取group
的顺序
df[match(df$group, category_order), ]
# group value
#1 tree 50
#3 house 2
#2 lake 1
#4 human 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.