Mixedorder data frame in R

前提是你 提交于 2019-12-20 06:05:02

问题


I have a data frame which looks like this :

a = c("1A","10A","11A","2B","2C","22C","3A","3B")
b= c(1,2,3,4,5,6,7,8)
ab = data.frame(a,b)

and I want to sort it according to the column a. I tried the mixed order

library(gtools)
ab[mixedorder(ab$a),]

but I don' t get the result I want (1A,2B,2C,3A,3B..). How can I fix this?


回答1:


We need to convert to character class (as the 'a' column is factor based on the default option in data.frame call i.e. stringsAsFactors=TRUE)

ab[mixedorder(as.character(ab$a)),]
#    a b
#1  1A 1
#4  2B 4
#5  2C 5
#7  3A 7
#8  3B 8
#2 10A 2
#3 11A 3
#6 22C 6

Additional options are also explained in here




回答2:


I'd just split the column. If you want numeric order for the numbers part of the ID, you should probably have these numbers as a numeric column anyway.

ab$a1 <- as.numeric(gsub("[[:alpha:]+]", "", ab$a))
ab$a2 <- gsub("\\d+", "", ab$a)

ab[order(ab$a1, ab$a2),]
#    a b a1 a2
#1  1A 1  1  A
#4  2B 4  2  B
#5  2C 5  2  C
#7  3A 7  3  A
#8  3B 8  3  B
#2 10A 2 10  A
#3 11A 3 11  A
#6 22C 6 22  C


来源:https://stackoverflow.com/questions/39270292/mixedorder-data-frame-in-r

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