read.csv() with UTF-8 encoding [duplicate]

我们两清 提交于 2020-02-20 08:01:05

问题


I am trying to read in data from a csv file and specify the encoding of the characters to be UTF-8. From reading through the ?read.csv() instructions, it seems that fileEncoding set equal to UTF-8 should accomplish this, however, I am not seeing that when checking. Is there a better way to specify the encoding of character strings to be UTF-8 when importing the data?

Sample Data:

Download Sample Data here

fruit<- read.csv("fruit.csv", header = TRUE, fileEncoding = "UTF-8")
fruit[] <- lapply(fruit, as.character)
Encoding(fruit$Fruit)

The output is "uknown" but I would expect this to be "UTF-8". What is the best way to ensure all imported characters are UTF-8? Thank you.


回答1:


fruit       <- read.csv("fruit.csv", header = TRUE)
fruit[]     <- lapply(fruit, as.character)
fruit$Fruit <- paste0(fruit$Fruit, "\xfcmlaut") # Get non-ASCII char and jam it in!
Encoding(fruit$Fruit)

[1] "latin1" "latin1" "latin1"

fruit$Fruit <- enc2utf8(fruit$Fruit)
Encoding(fruit$Fruit)

[1] "UTF-8" "UTF-8" "UTF-8"



来源:https://stackoverflow.com/questions/38986909/read-csv-with-utf-8-encoding

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