我在data.table包中发现setkey
函数不对像base::sort
函数这样的字符值进行排序。 setkey
中字符排序背后的逻辑是什么,以及如何使setkey
和sort
相互一致?
temp_dt <- data.table(index = c(letters[1:5], LETTERS[1:5]))
print(temp_dt$index)
# [1] "a" "b" "c" "d" "e" "A" "B" "C" "D" "E"
setkey(temp_dt, index)
print(temp_dt$index)
# [1] "A" "B" "C" "D" "E" "a" "b" "c" "d" "e"
sort(temp_dt$index)
# [1] "a" "A" "b" "B" "c" "C" "d" "D" "e" "E"
# different from `setkey` results.