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内部根据其逻辑对矢量进行排序,但是很难以它的创建方式显示它(并且在输出中可以看到)。
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)
?这样,矢量的排序和输出将是同步的。
答案 0 :(得分:15)
使用gtools包中的mixedsort
(或)mixedorder
函数:
require(gtools)
mixedorder(df.sorted)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
[28] 28 29 30 31 32 33 34 35 36 37 38 39
答案 1 :(得分:12)
将其构建为有序因子:
> 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 ...
编辑:
在@DWins发表评论之后,我想补充说,如果你给出正确的级别顺序,那么它就是一个有序的因素,这个因素就足够了:
> 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 ...
当您在回归分析中使用这些因素时,差异会很明显,可以区别对待它们。有序因子的优点是它们允许你使用比较运算符&lt;和&gt;。这使生活有时变得容易多了。
> 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
答案 2 :(得分:10)
这与您在ls
高于walker10_foo sorts
的所有词典短片(例如目录walker1_foo
)上获得的不完全相同吗?
在我的书中,最简单的方法是使用一致的数字位数,即我将更改为binned_walker01_1.grd
,依此插入0来表示一位数。
答案 3 :(得分:7)
回应Dwin对Dirk答案的评论:数据总是在你手上。 “这是R.如果没有,那就没有。” - 西蒙布隆伯格
您可以像这样添加0
:
df.sorted <- gsub("(walker)([[:digit:]]{1}_)", "\\10\\2", df.sorted)
如果您需要添加00
,请执行以下操作:
df.sorted <- gsub("(walker)([[:digit:]]{1}_)", "\\10\\2", df.sorted)
df.sorted <- gsub("(walker)([[:digit:]]{2}_)", "\\10\\2", df.sorted)
......等等。