我想使用这些功能重新调平() and 重新排序()在我的数据框中。我了解重新调整级别的工作原理,但我不明白为什么我在 data.frame 中看不到级别的变化。例如,假设我有鸢尾花数据集。
library(tidyverse)
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
iris$Species <- factor(iris$Species, levels = c("versicolor","setosa","virginica"),
labels = c("versicolor","setosa","virginica"))
Created on 2022-04-12 by the reprex package https://reprex.tidyverse.org (v2.0.1)
我可以使用此功能来更改级别的顺序
或 dplyr 中的此函数:
iris %>%
mutate(Species=factor(Species)) %>%
mutate(Species=fct_relevel(Species,c("versicolor","setosa","virginica"))) %>%
head()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
Created on 2022-04-12 by the reprex package https://reprex.tidyverse.org (v2.0.1)
What I do not get is that while I see the change in the levels in my data set, when i call my data set i do not see the change of the order, which is essential to me.
This is what I see
Species
setosa
...
versicolor
...
virginica
...
这就是我想看到的
Species
versicolor
...
setosa
...
virginica
...
任何使用 Tidyverse 更改顺序的帮助都将受到赞赏。