R: Sorting all columns in data frame by an alphanumeric column

倾然丶 夕夏残阳落幕 提交于 2021-02-05 10:37:15

问题


I want to sort all columns of a data frame in R by a column containing alphanumeric data. Here is an example data frame:

R> dd <- data.frame(b = c("Hi", "Med", "Hi", "Low"),
                 x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9),
                 z = c("A1", "A3", "A10", "A2"))

1   Hi  A   8   A1
2   Med D   3   A3
3   Hi  A   9   A10
4   Low C   9   A2

I would like to sort the entire data frame on column z. The desired output looks like this - with the info across columns staying consistent:

1   Hi  A   8   A1
2   Low C   9   A2
3   Med D   3   A3
4   Hi  A   9   A10

Here are the methods I've tried so far that have not worked:

Method 1: "A10" is incorrectly sorted

R> dd<- dd[with(dd, order(z)), ]
R> View(dd)
1   Hi  A   8   A1
4   Hi  A   9   A10
2   Low C   9   A2
3   Med D   3   A3

Method 2: No sort performed

R> library(gtools)
R> dd$z = factor(dd$z, levels = gtools::mixedsort(dd$z))
R> View(dd)
1   Hi  A   8   A1
2   Med D   3   A3
3   Hi  A   9   A10
4   Low C   9   A2

Method 3: No sort performed

R> library(stringr);
R> dd$z = factor(dd$z, levels = str_sort(dd$z, numeric=TRUE))
R> View(dd)
1   Hi  A   8   A1
2   Med D   3   A3
3   Hi  A   9   A10
4   Low C   9   A2

回答1:


I found a solution that works consistently for the example provided as well as my real data. Thanks to @rawr for the insight. When I create or load in data, I have to set stringAsFactors to false.

R> dd <- data.frame(b = c("Hi", "Med", "Hi", "Low"),
                 x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9),
                 z = c("A1", "A3", "A10", "A2"), stringsAsFactors = F)
R> dd

    b x y   z
1  Hi A 8  A1
2 Med D 3  A3
3  Hi A 9 A10
4 Low C 9  A2

R> library(gtools)
R> dd <- dd[gtools::mixedorder(dd$z), ]
R> dd

    b x y   z
1  Hi A 8  A1
4 Low C 9  A2
2 Med D 3  A3
3  Hi A 9 A10



回答2:


Here is an option with mixedorder

library(dplyr)
dd <- dd %>% 
         arrange(gtools::mixedorder(z))
dd
#   b x y   z
#1  Hi A 8  A1
#2 Low C 9  A2
#3 Med D 3  A3
#4  Hi A 9 A10


来源:https://stackoverflow.com/questions/58531533/r-sorting-all-columns-in-data-frame-by-an-alphanumeric-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!