stringr-----str_sort
主页:https://cran.r-project.org/web/packages/stringr/index.html
#安装stringr包> install.packages('stringr')> library(stringr)
#stringr函数分类:
字符串拼接函数
字符串计算函数
字符串匹配函数
字符串变换函数
参数控制函数
#stringr字符串计算函数
str_sort(x, decreasing = FALSE, na_last = TRUE, locale = "", ...) str_order(x, decreasing = FALSE, na_last = TRUE, locale = "", ...)
x: 字符串,字符串向量。 decreasing: 排序方式,默认为升序。
na_last:NA值的存放位置,一共3个值,TRUE放到最后,FALSE放到最前,
NA过滤处理 locale:按哪种语言习惯排序
#对字符串值进行排序
# 按ASCII字母排序 > str_sort(c('a',1,2,'11'), locale = "en") [1] "1" "11" "2" "a" # 倒序排序 > str_sort(letters,decreasing=TRUE) [1] "z" "y" "x" "w" "v" "u" "t" "s" "r" "q" "p" "o" "n" "m" "l" "k" "j" "i" "h" [20] "g" "f" "e" "d" "c" "b" "a" # 按拼音排序 > str_sort(c('你','好','粉','丝','日','志'),locale = "zh") [1] "粉" "好" "你" "日" "丝" "志"
#对NA值的排序处理
#把NA放最后面 > str_sort(c(NA,'1',NA),na_last=TRUE) [1] "1" NA NA #把NA放最前面 > str_sort(c(NA,'1',NA),na_last=FALSE) [1] NA NA "1" #去掉NA值 > str_sort(c(NA,'1',NA),na_last=NA) [1] "1" ### str_order 和 str_sort的区别 ### 在于前者返回排序后的索引(下标),后者返回排序后的实际值 > str_order(letters, locale = 'en') [1] 1 2 3 4 5 6 7 8 9 10 11 12 [13] 13 14 15 16 17 18 19 20 21 22 23 24 [25] 25 26 > str_sort(letters, locale = 'en') [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" [10] "j" "k" "l" "m" "n" "o" "p" "q" "r" [19] "s" "t" "u" "v" "w" "x" "y" "z"