You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

dplyr::arrange()处理希腊语文本排序异常,sort()正常,是何原因?

解决dplyr::arrange()对希腊语文本排序异常的问题

当使用dplyr::arrange()处理带重音的希腊语文本时,会出现排序结果不符合语言规则的情况,但基础R的sort()却能正确排序。以下是复现问题的示例代码:

library(dplyr)

wds <- structure(list(
  Word = c("θέση", "ταχυδρομείο", "γραμματόσημο", "προσπάθεια", "τηλεφωνώ", 
           "επικοινωνώ", "υπογραφή", "γλώσσα", "έξοδος", "μήνυμα", 
           "πραγματικότητα", "μυστικό", "διαδίκτυο", "δίκτυο"), 
  trans2 = c("the seat, position, job, post, station, status", 
             "the post office, postal service", "the postage stamp", 
             "the attempt (to do something), try (to accomplish something)",
             "to phone, to telephone (the act of calling someone)", 
             "to talk, interact, contact, communicate", "the signature, (fig) agreement",
             "the (anatomy, shoe, figuratively) tongue, language, (fish species) sole", 
             "the exit, outing, the act of going out, exodus",
             "the (lit or fig) message, email, text, SMS", 
             "the reality, actuality", "the secret, mystery", "the internet", 
             "the computer network")), row.names = c(NA, -14L), class = "data.frame")

# dplyr::arrange()的错误排序结果
wds %>% arrange(Word)
#              Word                                                                  trans2
# 1          έξοδος                          the exit, outing, the act of going out, exodus
# 2          γλώσσα the (anatomy, shoe, figuratively) tongue, language, (fish species) sole
# 3    γραμματόσημο                                                       the postage stamp
# 4          δίκτυο                                                    the computer network
# 5       διαδίκτυο                                                            the internet
# 6      επικοινωνώ                                 to talk, interact, contact, communicate
# 7            θέση                          the seat, position, job, post, station, status
# 8          μήνυμα                              the (lit or fig) message, email, text, SMS
# 9         μυστικό                                                     the secret, mystery
# 10 πραγματικότητα                                                  the reality, actuality
# 11     προσπάθεια            the attempt (to do something), try (to accomplish something)
# 12    ταχυδρομείο                                         the post office, postal service
# 13       τηλεφωνώ                     to phone, to telephone (the act of calling someone)
# 14       υπογραφή                                          the signature, (fig) agreement 

而基础R的sort()能给出符合希腊语规则的正确排序:

sort(wds$Word)

#  [1] "γλώσσα"         "γραμματόσημο"   "διαδίκτυο"      "δίκτυο"         "έξοδος"        
#  [6] "επικοινωνώ"     "θέση"           "μήνυμα"         "μυστικό"        "πραγματικότητα"
# [11] "προσπάθεια"     "ταχυδρομείο"    "τηλεφωνώ"       "υπογραφή"

解决方案

问题根源在于dplyr::arrange()默认使用的排序规则(通常是C locale)不支持希腊语的重音字符排序逻辑,而基础R的sort()会遵循系统当前的locale设置。可以通过以下几种方式解决:

方法1:结合match()和sort()实现正确排序

直接利用sort()的正确结果,通过match()生成排序索引:

wds %>% arrange(match(Word, sort(Word)))

方法2:设置系统locale为希腊语

修改系统的排序规则locale为希腊语,之后arrange()就能正常工作:

# Windows系统使用
Sys.setlocale(category = "LC_COLLATE", locale = "Greek_Greece.1253")
# Linux/macOS系统使用
Sys.setlocale(category = "LC_COLLATE", locale = "el_GR.UTF-8")

wds %>% arrange(Word)

方法3:使用stringr::str_sort()指定locale

借助stringr包的str_sort()函数,明确指定希腊语locale来排序:

library(stringr)
wds %>% arrange(str_sort(Word, locale = "el"))

内容的提问来源于stack exchange,提问作者Kat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 14:15:16