r 中的 Order() 函数排序不正确

问题描述 投票:0回答:2

我有一些数字,正在做一些订购。输出将 7 放在 70 旁边,就好像 7 是 70。为什么会发生这种情况。下面粘贴的内容是实际输出。请注意,263 是如何被视为小于 27 的,就好像 27 中的 7 后面有一个 0。4 在 38 之后,就好像 4 意味着 40。我正在使用 order()。

 feat_1  25
 feat_10  26
 feat_24 263
 feat_48  27
 feat_55  27
 feat_75  36
 feat_16  37
 feat_53  38
 feat_89  38
 feat_28   4
r
2个回答
9
投票

您的问题没有答案,请接受这个答案


2
投票

您还可以使用

mixedsort
包中的
mixedorder
gtools
(作为快速替代方案),并且无需将列转换为数字,因为它处理字符数字或字母数字字符串:

数据

df <- read.table(text='feat_1  25
 feat_10  "26"
 feat_24  "263"
 feat_48  "27"
 feat_55  "27"
 feat_75  "36"
 feat_16  "37"
 feat_53  "38"
 feat_89  "38"
 feat_28   "4"')

解决方案

library(gtools)
#you use mixedorder in exactly the same way as base order
> df[mixedorder(df$V2),]
        V1  V2
10 feat_28   4
1   feat_1  25
2  feat_10  26
4  feat_48  27
5  feat_55  27
6  feat_75  36
7  feat_16  37
8  feat_53  38
9  feat_89  38
3  feat_24 263
© www.soinside.com 2019 - 2024. All rights reserved.