Cannot read unicode .csv into R

风格不统一 提交于 2019-11-27 08:52:06
puslet88

I wrote a longer answer on the same issue here: R on Windows: character encoding hell .

Quick answer, using the parameter encoding instead of fileEncoding should fix your first issue. You will not be able to read it possibly in either console or table view in RStudio, but you will be able to use it in formulaes.

d <- read.csv("./Data/1.csv", encoding="UTF-8")
head(d)

Having saved your table into a UTF-8 file:

> test2 <- read.csv("test2.csv", header = FALSE, sep = ",", quote = "\"", dec = ".", fill = TRUE, comment.char = "", encoding = "UTF-8")
Warning message:
In read.table(file = file, header = header, sep = sep, quote = quote,  :
  incomplete final line found by readTableHeader on 'test2.csv'

This gives you how it looks like in the console and RStudio view

> test2
        V1       V2
1 <U+0531> <U+0532>
2        1       10
3        2       20

However importantly you are able to manipulate this within R. Thus in my case it is possible to see that the script window input Ա has UTF-8 encoding, and a grep correctly finds this encoding in your table.

> Encoding("Ա")
[1] "UTF-8"
> grep("Ա", as.character(test2[1,1]))
[1] 1

You may need to find suitable encoding variants that work on your settings, or possibly change them. Unfortunately I am not sure where it is done.

You might not be able to make it pretty in all stages, but it is definitely possible to get it to work also in Windows 7 environment.

I tried two ways to replicate your problem.

I copied the characters above into RStudio, saved it to a csv with this code:

write.csv(c("Ա","Բ",
             1,10,
             2,20), "test.csv")

df <- read.csv("test.csv")

This worked fine.

Then I thought, well maybe R is cheating when I save it to CSV with R? So I just pasted the characters to a text file and save it as a CSV. This approach doesn't have problems either.

Here's my session info:

sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8       
[4] LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
[7] LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] party_1.0-9       modeltools_0.2-21 strucchange_1.4-7 sandwich_2.2-10   zoo_1.7-10       
[6] GGally_0.4.4      reshape_0.8.4     plyr_1.8          ggplot2_0.9.3.1  

loaded via a namespace (and not attached):
[1] coin_1.0-23        colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3      
[5] gtable_0.1.2       labeling_0.2       lattice_0.20-23    MASS_7.3-29       
[9] munsell_0.4.2      mvtnorm_0.9-9995   proto_0.3-10       RColorBrewer_1.0-5
[13] reshape2_1.2.2     scales_0.2.3       splines_3.0.1      stringr_0.6.2 
Julien Colomb

I had the same problem and found out that the file was corrupted.

I opened the file with OpenOffice and saved it back using "UTF8" character set (you need to click the edit filter settings box) and then imported it with the read.csv()(no encoding or filencoding option) and it worked fine.

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