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("ب","الف")

list2 ##OK-readable

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

## not readable

## data frame with 0 columns and 2 rows  ???
  • 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:

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")

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.

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

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

  • $\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.