R print UTF-8 code in data.frames on Windows platform Rstudio

时间秒杀一切 提交于 2019-12-23 19:27:01

问题


When there are UTF-8 characters in the data frame, it won't be displayed properly.

For example, the following is correct:

> "\U6731"
[1] "朱"

But when I put that in a data frame and have it printed, here it is:

> data.frame(x="\U6731")
         x
1 <U+6731>

Hence I believe this has nothing to do with encoding issues.

Is there any direct way to print instead of <U+6731>.

I have to use Windows in company so using Linux might not be feasible for me.


回答1:


The corpus library has a work-around for this bug. Either do this:

library(corpus)
df <- data.frame(x = "\U6731")
print.corpus_frame(df)

Or else do this:

class(df) <- c("corpus_frame", "data.frame")
df



回答2:


You are right, while calling the whole dataframe it will give codes for UTF-8 characters:

> data.frame(x="\U6731")
         x
1 <U+6731>

But if you call for columns or rows, it would print nicely:

# through the column name
> data.frame(x="\U6731")$x
[1] 朱
Levels: 朱

# through the column index
> data.frame(x="\U6731")[,1]
[1] 朱
Levels: 朱

# through the row index
> data.frame(x="\U6731")[1,]
[1] 朱
Levels: 朱

Not sure if this helps. Could you be more specific why and how exactly you need to output these characters?



来源:https://stackoverflow.com/questions/44534690/r-print-utf-8-code-in-data-frames-on-windows-platform-rstudio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!