# Reading Persian Characters in R

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

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

testdf

testdf[,testdf$list2=="ب"] ## data frame with 0 columns and 2 rows ???  ## 3 Answers • 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 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: If you edit it in Notepad++ so that it looks like 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.

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

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

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