lacfo
lacfo

Reputation: 79

How to read CSV file containing Chinese characters?

My computer's information is as follows,

 > sessionInfo()
  R version 3.3.1 (2016-06-21)
  Platform: x86_64-apple-darwin13.4.0 (64-bit)
  Running under: OS X 10.11.5 (El Capitan)

  locale:
  [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8

  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     

  loaded via a namespace (and not attached):
  [1] tools_3.3.1

I want to read a csv file into my Rstudio by

dat <- read.csv("e.csv", fileEcoding = "utf-8")

The CSV file is like this,

enter image description here

And I got an error,

> dat
  postal_code province
1          10        p

The result is just a 1*2 data frame.

Upvotes: 0

Views: 2843

Answers (1)

Hack-R
Hack-R

Reputation: 23231

chinese <- data.frame(Postal=c(10,20,30),Province=c('中','華','民族'))
write.csv(chinese, "chinese.csv",row.names = F)

dat <- read.csv("chinese.csv")
dat # Here it is in Unicode
  Postal         Province
1     10         <U+4E2D>
2     20         <U+83EF>
3     30 <U+6C11><U+65CF>

If you still have trouble please provide your data and we can help further.

Another related example:

x=c('中華民族');x; y <- data.frame(x, stringsAsFactors=FALSE) 
y
                                 x
1 <U+4E2D><U+83EF><U+6C11><U+65CF>
x

[1] "中華民族"

Upvotes: 2

Related Questions