本文介绍了重新排序数据框中的因子水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的 data.frame:
I have a data.frame as shown below:
task measure
right m1
left m2
up m3
down m4
front m5
back m6
.
.
.
任务列只取六个不同的值,它们被视为因子,并按 R 排序为:后"、下"、前"、左"、右"、上".
The task column takes only six different values, which are treated as factors, and are ordered by R as: "back", "down", "front", "left", "right", "up".
但是,我需要将它们排序为:上"、下"、左"、右"、前"、后".这样当我在 ggplot 中使用这些数据时,相关的任务(例如向上"和向下")会彼此相邻绘制.
How ever, I need them ordered as: "up", "down", "left", "right", "front", "back". So that when I use this data in ggplot, the related tasks (such as "up" and "down") are plotted next to each other.
如何改变因子任务"的级别顺序?
How can change the ordering of the levels of the factor "task"?
推荐答案
假设你的数据框是 mydf:
Assuming your dataframe is mydf:
mydf$task <- factor(mydf$task, levels = c("up", "down", "left", "right", "front", "back"))
这篇关于重新排序数据框中的因子水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!