本文介绍了数据帧中的重新排序因子级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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定为:back,down,front,left,right,up。

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中使用这些数据时,相关的任务(例如up和down)被相互绘制。

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.

因素任务的级别排序?

推荐答案

假设您的数据框是mydf:

Assuming your dataframe is mydf:

mydf$task <- factor(mydf$task, levels = c("up", "down", "left", "right", "front", "back"))

这篇关于数据帧中的重新排序因子级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 02:05