按R中的数据重新排序因子水平

时间:2022-02-14 14:58:07

Suppose I have a factor whose levels are

假设我有一个级别为的因子

"1/1/2013" "1/1/2014" "1/1/2015" "10/1/2012" "10/1/2013" "10/1/2014" "4/1/2013" "4/1/2014" "4/1/2015" "7/1/2012" "7/1/2013" "7/1/2014"

“1/1/2013”​​“1/1/2014”“1/1/2015”“10/1/2012”“10/1/2013”​​“10/1/2014”“4/1/2013”​​“ 2014年4月1日“”2015年4月1日“”7/1/2012“”2013年7月1日“”2014年7月1日“

What is the easiest way to resort this by date? I know I can do this manually by picking and choosing...

到目前为止,最简单的方法是什么?我知道我可以通过选择和选择手动完成这个...

Thanks!

谢谢!

1 个解决方案

#1


1  

This will sort the levels of the factor so that plotting will make sense, but ordinary factors do not really have an order. For there to really be order, i.e. for there to be a less-than or greater-than relationship between items, you would need to define an "ordered" factor (see ?factor):

这将对因子的级别进行排序,以便绘图有意义,但普通因素并不真正有序。为了真正有秩序,即项目之间的关系小于或大于,你需要定义一个“有序”因子(参见?factor):

> fac <- factor(c( "1/1/2013",  "1/1/2014",  "1/1/2015",  "10/1/2012", "10/1/2013" ,"10/1/2014", "4/1/2013",  "4/1/2014" , "4/1/2015" , "7/1/2012",  "7/1/2013",  "7/1/2014") )
> levels(fac) <- levels(fac)[ order( as.Date(levels(fac), format="%m/%d/%Y") )]
> fac
 [1] 7/1/2012  10/1/2012 1/1/2013  4/1/2013  7/1/2013  10/1/2013
 [7] 1/1/2014  4/1/2014  7/1/2014  10/1/2014 1/1/2015  4/1/2015 
12 Levels: 7/1/2012 10/1/2012 1/1/2013 4/1/2013 ... 4/1/2015

Now the levels will display in the order you expect:

现在,级别将按您期望的顺序显示:

> levels(fac)
 [1] "7/1/2012"  "10/1/2012" "1/1/2013"  "4/1/2013"  "7/1/2013" 
 [6] "10/1/2013" "1/1/2014"  "4/1/2014"  "7/1/2014"  "10/1/2014"
[11] "1/1/2015"  "4/1/2015" 

But if there are repeated elements, then the levels will be shorter than the factor vector itself and they are still not "in order" in the vector itself, ... because it's not an ordered factor. It would make a lot more sense to convert that vector to Date-class.

但是如果有重复的元素,那么级别将比因子向量本身短,并且它们在向量本身中仍然不是“按顺序”,因为它不是有序因子。将该向量转换为Date-class会更有意义。

Or you could ignore all my advice and just do what you asked to do:

或者你可以忽略我的所有建议,只做你要求做的事情:

> fac[ order( as.Date(fac, format="%m/%d/%Y") )]
 [1] 7/1/2012  10/1/2012 1/1/2013  4/1/2013  7/1/2013  10/1/2013
 [7] 1/1/2014  4/1/2014  7/1/2014  10/1/2014 1/1/2015  4/1/2015 
12 Levels: 1/1/2013 1/1/2014 1/1/2015 10/1/2012 ... 7/1/2014

#1


1  

This will sort the levels of the factor so that plotting will make sense, but ordinary factors do not really have an order. For there to really be order, i.e. for there to be a less-than or greater-than relationship between items, you would need to define an "ordered" factor (see ?factor):

这将对因子的级别进行排序,以便绘图有意义,但普通因素并不真正有序。为了真正有秩序,即项目之间的关系小于或大于,你需要定义一个“有序”因子(参见?factor):

> fac <- factor(c( "1/1/2013",  "1/1/2014",  "1/1/2015",  "10/1/2012", "10/1/2013" ,"10/1/2014", "4/1/2013",  "4/1/2014" , "4/1/2015" , "7/1/2012",  "7/1/2013",  "7/1/2014") )
> levels(fac) <- levels(fac)[ order( as.Date(levels(fac), format="%m/%d/%Y") )]
> fac
 [1] 7/1/2012  10/1/2012 1/1/2013  4/1/2013  7/1/2013  10/1/2013
 [7] 1/1/2014  4/1/2014  7/1/2014  10/1/2014 1/1/2015  4/1/2015 
12 Levels: 7/1/2012 10/1/2012 1/1/2013 4/1/2013 ... 4/1/2015

Now the levels will display in the order you expect:

现在,级别将按您期望的顺序显示:

> levels(fac)
 [1] "7/1/2012"  "10/1/2012" "1/1/2013"  "4/1/2013"  "7/1/2013" 
 [6] "10/1/2013" "1/1/2014"  "4/1/2014"  "7/1/2014"  "10/1/2014"
[11] "1/1/2015"  "4/1/2015" 

But if there are repeated elements, then the levels will be shorter than the factor vector itself and they are still not "in order" in the vector itself, ... because it's not an ordered factor. It would make a lot more sense to convert that vector to Date-class.

但是如果有重复的元素,那么级别将比因子向量本身短,并且它们在向量本身中仍然不是“按顺序”,因为它不是有序因子。将该向量转换为Date-class会更有意义。

Or you could ignore all my advice and just do what you asked to do:

或者你可以忽略我的所有建议,只做你要求做的事情:

> fac[ order( as.Date(fac, format="%m/%d/%Y") )]
 [1] 7/1/2012  10/1/2012 1/1/2013  4/1/2013  7/1/2013  10/1/2013
 [7] 1/1/2014  4/1/2014  7/1/2014  10/1/2014 1/1/2015  4/1/2015 
12 Levels: 1/1/2013 1/1/2014 1/1/2015 10/1/2012 ... 7/1/2014