R自行对向量排序 [英] R sorts a vector on its own accord

查看:167
本文介绍了R自行对向量排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

df.sorted <- c("binned_walker1_1.grd", "binned_walker1_2.grd", "binned_walker1_3.grd",
    "binned_walker1_4.grd", "binned_walker1_5.grd", "binned_walker1_6.grd",
    "binned_walker2_1.grd", "binned_walker2_2.grd", "binned_walker3_1.grd",
    "binned_walker3_2.grd", "binned_walker3_3.grd", "binned_walker3_4.grd",
    "binned_walker3_5.grd", "binned_walker4_1.grd", "binned_walker4_2.grd",
    "binned_walker4_3.grd", "binned_walker4_4.grd", "binned_walker4_5.grd",
    "binned_walker5_1.grd", "binned_walker5_2.grd", "binned_walker5_3.grd",
    "binned_walker5_4.grd", "binned_walker5_5.grd", "binned_walker5_6.grd",
    "binned_walker6_1.grd", "binned_walker7_1.grd", "binned_walker7_2.grd",
    "binned_walker7_3.grd", "binned_walker7_4.grd", "binned_walker7_5.grd",
    "binned_walker8_1.grd", "binned_walker8_2.grd", "binned_walker9_1.grd",
    "binned_walker9_2.grd", "binned_walker9_3.grd", "binned_walker9_4.grd",
    "binned_walker10_1.grd", "binned_walker10_2.grd", "binned_walker10_3.grd")

人们希望该向量的顺序为1:length(df.sorted),但事实并非如此.看起来R会根据其逻辑在内部对向量进行排序,但实际上很难显示它的创建方式(并在输出中看到).

One would expect that order of this vector would be 1:length(df.sorted), but that appears not to be the case. It looks like R internally sorts the vector according to its logic but tries really hard to display it the way it was created (and is seen in the output).

order(df.sorted)
 [1] 37 38 39  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
[26] 23 24 25 26 27 28 29 30 31 32 33 34 35 36

有没有办法将顺序重置"为1:length(df.sorted)?这样,排序和向量的输出将同步.

Is there a way to "reset" the ordering to 1:length(df.sorted)? That way, ordering, and the output of the vector would be in sync.

推荐答案

将其构造为有序因素:

> df.new <- ordered(df.sorted,levels=df.sorted)
> order(df.new)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 ...



EDIT :

在@DWins评论之后,我想补充一点,即使将其设为有序因素也不是必需的,如果您给出正确的级别顺序,仅一个因素就足够了:

After @DWins comment, I want to add that it is even not nessecary to make it an ordered factor, just a factor is enough if you give the right order of levels :

>     df.new2 <- factor(df.sorted,levels=df.sorted)
>     order(df.new)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 ...

当您在回归分析中使用这些因素时,差异会很明显,可以将它们区别对待.有序因子的优点是它们使您可以将比较运算符用作<和>.这有时会使生活变得更轻松.

The difference will be noticeable when you use those factors in a regression analysis, they can be treated differently. The advantage of ordered factors is that they let you use comparison operators as < and >. This makes life sometimes a lot easier.

> df.new2[5] < df.new2[10]
[1] NA
Warning message:
In Ops.factor(df.new[5], df.new[10]) : < not meaningful for factors

> df.new[5] < df.new[10]
[1] TRUE

这篇关于R自行对向量排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆