对由数字组成的因子/字符列进行排序

时间:2016-04-29 06:50:17

标签: r

我无法根据列ID对data.frame进行排序。

以下是示例数据:

data <- structure(list(ID = c("542_1_3", "542_1_3", "542_1_3", "542_1_11", 
                              "542_1_11", "542_1_11", "542_1_14", "542_1_14", "542_1_14", "542_1_14", 
                              "542_1_14", "542_1_3", "542_1_3", "542_1_3", "542_1_11", "542_1_11", 
                              "542_1_11", "542_1_14", "542_1_14", "542_1_14", "542_1_14", "542_2_1"
), X = c(17.55, 23.75, 387.9, 148.3, 300.6, 413.1, 
                       41.85, 148.4, 237.7, 301.6, 414.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                       0, 0), Y = c(931, 924.2, 632, 789.8, 689.9, 637.8, 901, 
                                          809.5, 735.4, 675, 637, 953, 953, 953, 968.5, 968.5, 968.5, 959, 
                                          959, 959, 959, 959)), .Names = c("ID", "X", "Y"
                                          ), row.names = c("1", "2", "3", "8", "9", "10", "11", "12", "13", 
                                                           "14", "15", "16", "21", "31", "81", "91", "101", "111", "121", 
                                                           "131", "141", "151"), class = "data.frame")

我在我的闪亮应用程序中使用此数据,并且在根据ID列进行绘图时,图例会混淆并且值不会被排序。例如,而不是获取图例的传奇ID顺序:

"542_1_3", "542_1_11", "542_1_14","542_2_1"

我明白了:

"542_1_11", "542_1_14", "542_1_3","542_2_1"

我无法手动设置ID级别,因为由于此数据属于被动图,因此选择的ID值与级别数相关的级别变化太高。

[编辑]

以下是更好的可重复示例:

data <- structure(list(ID = structure(1:3, .Label = c("544_1_11", "544_1_3", 
                                                      "544_1_7"), class = "factor"), `T8/5.5 Zeit [s]` = c(NA_real_, 
                                                                                                           NA_real_, NA_real_), Value = c(968, 949, 972)), .Names = c("ID", 
                                                                                                                                                                      "T8/5.5 Zeit [s]", "Value"), row.names = c(NA, -3L), class = "data.frame")

而代码:

data$ID <- with(data, factor(ID, levels = unique(ID)))

不会更改ID列的级别

levels(data$ID)
[1] "544_1_11" "544_1_3"  "544_1_7"

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我们可以转换为factorlevels指定为unique元素

data$ID <- with(data, factor(ID, levels = unique(ID)))
levels(data$ID)
#[1] "542_1_3"  "542_1_11" "542_1_14" "542_2_1" 

更新

在新数据集中,unique ID的顺序不正确。因此,我们可以使用mixedsort gtools来识别&#39; ID&#39;并在levels电话中将其用作factor

library(gtools)
data$ID <- with(data, factor(ID, levels = mixedsort(as.character(ID))))