-
str_sort()
返回排序後的向量。 -
str_order()
返回一個整數向量,該整數向量在用於取子集時返回所需的順序,即x[str_order(x)]
與str_sort()
相同 -
str_rank()
返回值的排名,即arrange(df, str_rank(x))
與str_sort(df$x)
相同。
用法
str_order(
x,
decreasing = FALSE,
na_last = TRUE,
locale = "en",
numeric = FALSE,
...
)
str_rank(x, locale = "en", numeric = FALSE, ...)
str_sort(
x,
decreasing = FALSE,
na_last = TRUE,
locale = "en",
numeric = FALSE,
...
)
參數
- x
-
要排序的字符向量。
- decreasing
-
一個布爾值。如果是
FALSE
(默認值),則從最低到最高排序;如果TRUE
從最高到最低排序。 - na_last
-
NA
應該去哪裏?TRUE
位於末尾,FALSE
位於開頭,NA
已刪除。 - locale
-
用於比較的區域設置。有關所有可能的選項,請參閱
stringi::stri_locale_list()
。默認為 "en"(英語),以確保默認行為在不同平台上保持一致。 - numeric
-
如果
TRUE
,將按數字排序數字,而不是字符串。 - ...
-
用於控製排序規則的其他選項。傳遞給
stringi::stri_opts_collator()
。
也可以看看
stringi::stri_order()
用於底層實現。
例子
x <- c("apple", "car", "happy", "char")
str_sort(x)
#> [1] "apple" "car" "char" "happy"
str_order(x)
#> [1] 1 2 4 3
x[str_order(x)]
#> [1] "apple" "car" "char" "happy"
str_rank(x)
#> [1] 1 2 4 3
# In Czech, ch is a digraph that sorts after h
str_sort(x, locale = "cs")
#> [1] "apple" "car" "happy" "char"
# Use numeric = TRUE to sort numbers in strings
x <- c("100a10", "100a5", "2b", "2a")
str_sort(x)
#> [1] "100a10" "100a5" "2a" "2b"
str_sort(x, numeric = TRUE)
#> [1] "2a" "2b" "100a5" "100a10"
相關用法
- R stringr str_which 查找匹配索引
- R stringr str_extract 提取完整的匹配項
- R stringr str_subset 查找匹配元素
- R stringr str_escape 轉義正則表達式元字符
- R stringr str_trim 刪除空格
- R stringr str_sub 使用子字符串的位置獲取和設置子字符串
- R stringr str_replace_na 把NA變成“NA”
- R stringr str_trunc 將字符串截斷至最大寬度
- R stringr str_match 從匹配中提取組件(捕獲組)
- R stringr str_like 以與 SQL 的 LIKE 運算符相同的方式檢測模式
- R stringr str_length 計算長度/寬度
- R stringr str_detect 檢測是否存在匹配
- R stringr str_count 計算匹配次數
- R stringr str_split 將字符串分成幾段
- R stringr str_unique 刪除重複的字符串
- R stringr str_remove 刪除匹配的模式
- R stringr str_pad 將字符串填充到最小寬度
- R stringr str_equal 判斷兩個字符串是否相等
- R stringr str_view 查看字符串和匹配項
- R stringr str_glue 用膠水插補
- R stringr str_conv 指定字符串的編碼
- R stringr str_starts 檢測開始/結束時是否存在匹配
- R stringr str_c 將多個字符串連接成一個字符串
- R stringr str_wrap 將單詞包裝成格式良好的段落
- R stringr str_dup 複製字符串
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Order, rank, or sort a character vector。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。