Shr*_*Tan 8 encoding r data.table
好吧,我已经在Github上提交了这个问题,但没有得到回复.data.table是一个伟大的R包,在日常工作中帮助我们很多.
但是,在版本1.9.6之后,如果列没有以UTF-8编码(R中的默认非ASCII字符编码取决于平台),它突然不支持Windows上的非ASCII键.
这很可能是一个错误(我会说一个大错误).我很惊讶,没有人关注这个,因为这个bug已经存在了将近2年,所以没有人抱怨.
我花了好几个小时试图解决这个问题,但都失败了.相关提交是https://github.com/Rdatatable/data.table/commit/03cd45f83fe41e4a6507b9b2e4f955c105979c8c和https://github.com/Rdatatable/data.table/commit/409d709380e865d014f21f17a254e0bbcf1e156d
他们实际上是在尝试将其他编码字符转换为UTF-8,然后对UTF-8中的所有字符进行排序和比较.似乎编码处理是正确的.但是,我确实怀疑这个bug被隐藏了.实施data.table真的很复杂,我问是否有人可以提供帮助,以便我们可以制定公关来解决这个问题.
非常感谢.
Minimal reproducible examplelibrary(data.table)
## data.table 1.10.5 IN DEVELOPMENT built 2017-12-01 20:06:10 UTC
## The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way
## Documentation: ?data.table, example(data.table) and browseVignettes("data.table")
## Release notes, videos and slides: http://r-datatable.com
dt <- data.table(
x = c("????????", "????", "????", "??????", "??????"),
y = 1:5,
key = "x"
)
Run Code Online (Sandbox Code Playgroud)
dt[]
## x y
## 1: ???????? 1
## 2: ???? 2
## 3: ???? 3
## 4: ?????? 4
## 5: ?????? 5
Encoding(dt$x)
## [1] "unknown" "unknown" "unknown" "unknown" "unknown"
dt[J("????????")][]
## x y
## 1: ???????? NA
Run Code Online (Sandbox Code Playgroud)
现在它返回正确的答案1.注意dt的顺序现在也变得不同了,这是不应该发生的.
dt[, x := enc2utf8(x)]
setkey(dt, x)
dt[]
## x y
## 1: ???? 3
## 2: ???????? 1
## 3: ?????? 4
## 4: ???? 2
## 5: ?????? 5
Encoding(dt$x)
## [1] "UTF-8" "UTF-8" "UTF-8" "UTF-8" "UTF-8"
dt[J("????????")][]
## x y
## 1: ???????? 1
Run Code Online (Sandbox Code Playgroud)
sessionInfo()
## R version 3.4.1 (2017-06-30)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936
## [2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936
## [3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_People's Republic of China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] data.table_1.10.5
##
## loaded via a namespace (and not attached):
## [1] compiler_3.4.1 backports_1.1.1 magrittr_1.5 rprojroot_1.2
## [5] tools_3.4.1 htmltools_0.3.6 Rcpp_0.12.13 stringi_1.1.5
## [9] rmarkdown_1.8 knitr_1.17 stringr_1.2.0 digest_0.6.12
## [13] evaluate_0.10.1
Run Code Online (Sandbox Code Playgroud)
因为这个问题已经解决了PR,所以我正在回答我自己的问题.
对于字符串,请data.table以UTF8编码比较它们的值.但是,由于缺少两个ENC2UTF8in csort()和csort_pre(),data.table创建的顺序实际上取决于编码.在Windows上,当键中存在字符串时,默认编码不是UTF8这一事实会导致一些奇怪的输出.
为了调试这种情况,您需要知道如何将C例程中的非ASCII字符打印到R的输出.Rprintf()直接使用你会弄得一团糟.你必须先translateChar()在字符串上使用.
参考文献:
| 归档时间: |
|
| 查看次数: |
155 次 |
| 最近记录: |