2
$\begingroup$

I have a csv file with characters in Persian and I cannot view them in R correctly. Also, I cannot subset based on Persian characters values. Here is a sample code:

list1 <- c("x","y")
list2 <- c("ب","الف")

list1 
list2 ##OK-readable

writedf <- as.data.frame(cbind(list1,list2),encoding="UTF-8")
write.csv(writedf,"test.csv")
testdf <- read.csv("test.csv",encoding="UTF-8")

testdf  
## not readable

testdf[,testdf$list2=="ب"]
## data frame with 0 columns and 2 rows  ???
$\endgroup$
1
$\begingroup$
  • make sure the files are saved in UTF
  • try Sys.setlocale("LC_ALL", locale_code) and have a look at the documentation of this function
$\endgroup$
1
$\begingroup$

First of all I should mention that this is an OS specific question. You will see this problem in Windows but not in Linux (I'm not sure about OS X, but maybe no problem there too.)

The problem with your code is not with the reading part. If you open your test.csv in Notepad++ you will see something like this:

file written from R

If you edit it in Notepad++ so that it looks like

file edited in Notepad++

and then you run this:

testdf <- read.csv("test.csv",encoding="UTF-8")
testdf[1,3]
testdf$list2
plot(1:10,main=testdf2[2,3])

You will see that there is no problem.

So in fact there are two problems:

  1. When writing Unicode text, R changes it back to system locale and then again to unicode, that's why writing data to csv file does not work correctly.

  2. R has problems showing Unicode data in data.frame so even after reading it correctly, testdf would print those codes there not the Unicode characters.

see this question on Writing Unicode Text into Text File from R (in Windows) in R mailing list archives, and also this question on UTF-8 file output in R on stackoverflow.

$\endgroup$
  • $\begingroup$ and I think@mike-wise is right about the correct place for this question. $\endgroup$ – Majid Einian Jun 22 '15 at 13:01
0
$\begingroup$

Use subset instead i.e. subset(testdf, testdf$list2=="ب") results in what you want.

$\endgroup$
  • $\begingroup$ subset did not solve the problem. $\endgroup$ – Hamideh Jun 19 '15 at 8:13
  • $\begingroup$ Well, it worked for me! so try Sys.setlocale and see if it helps. $\endgroup$ – Ehsan M. Kermani Jun 19 '15 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.