<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/hebrew-using-hebrew-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-posts.com/hebrew-using-hebrew-in-r/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h4><em>Adi Sarid </em><em>(Tel Aviv university and Sarid Research Institute LTD.)</em></h4>
<h4><em>July-2017</em></h4>
<div>
<h1>Background</h1>
<p>A while back I participated in an R workshop, in the annual convention of the Israeli Association for Statistics. I had the pleasure of talking with <a rel="nofollow" href="https://www.r-statistics.com/about/" target="_blank">Tal Galili</a> and <a rel="nofollow" href="http://www.john-ros.com/" target="_blank">Jonathan Rosenblatt</a> which indicated that a lot of Israeli R users run into difficulties with Hebrew with R.<br>
My firm opinion is that its best to keep everything in English, but sometimes you simply don’t have a choice. For example, I had to prepare a number of R shiny dashboards to Hebrew speaking clients, hence Hebrew was the way to go, in a kind of English-Hebrew “Mishmash” (mix).<br>
I happened to run into a lot of such difficulties in the past, so here are a few pointers to get you started when working in R with Hebrew.<br>
This post deals with Reading and writing files which contain Hebrew characters.<br>
Note, there is also a bit to talk about in the context of Shiny apps which contain Hebrew and using right-to-left in shiny apps, and using Hebrew variable names. Both work with some care, but I won’t cover them here.<br>
If you have any other questions you’d like to see answered, feel free to contact me <a rel="nofollow" href="mailto:adisarid@gmail.com" target="_blank">adisarid@gmail.com</a>.
</div>
<h1>Reading and writing files with Hebrew characters</h1>
<p>R can read and write files in many formats. The common formats for small to medium data sets include the comma separated values (*.csv), and excel files (*.xlsx, *.xls). Each such read/write action is facilitated using some kind of “encoding”. Encoding, in simple terms, is a definition of a character set which help you operating system to interpret and represent the character as it should (לדוגמה, תווים בעברית). There are a number of relevant character sets (encodings) when Hebrew is concerned:</p>
<ul>
<li>UTF-8</li>
<li>ISO 8859-8</li>
<li>Windows-1255</li>
</ul>
<p>When you try to read files in which there are Hebrew characters, I usually recommend trying to read them in that order- UTF-8 is commonly used by a lot of applications, since it covers a lot of languages.</p>
<div>
<h2>Using csv files with Hebrew characters</h2>
<p>Here’s an example for something that can go wrong, and a possible solution. In this case I’ve prepared a csv file which encoded with UTF-8. When using R’s standard <code>read.csv</code> function, this is what happens:</p>
<pre><code>sample.data &lt;- read.csv(&quot;http://www.sarid-ins.co.il/files/utf_encoded_sample.csv&quot;)
sample.data</code></pre>
<pre>##    ן...... X......        X............
## 1 ׳¨׳•׳ ׳™      25             ׳—׳™׳₪׳”
## 2 ׳ž׳•׳˜׳™      77         ׳”׳¨׳¦׳œ׳™׳”
## 3   ׳“׳ ׳™      13 ׳×׳œ-׳ ׳‘׳™׳‘ ׳™׳₪׳•
## 4 ׳¨׳¢׳•׳×      30  ׳§׳¨׳™׳× ׳©׳ž׳•׳ ׳”
## 5   ׳“׳ ׳”      44        ׳‘׳™׳× ׳©׳ ׳Ÿ</pre>
<p>Oh boy, that’s probably not what the file’s author had in mind. Let’s try to instruct <code>read.csv</code> to use a different encoding.</p>
<pre><code>sample.data &lt;- read.csv(&quot;http://www.sarid-ins.co.il/files/utf_encoded_sample.csv&quot;,
                        encoding = &quot;UTF-8&quot;)
sample.data</code></pre>
<pre>##   X.U.FEFF.שם גיל      מגורים
## 1        רוני  25        חיפה
## 2        מוטי  77      הרצליה
## 3         דני  13 תל-אביב יפו
## 4        רעות  30  קרית שמונה
## 5         דנה  44     בית שאן</pre>
<p>A bit better isn’t it? However, not perfect. We can read the Hebrew, but there is a weird thing in the header “X.U.FEFF”.<br>
A better way to read and write files (much more than just encoding aspects – it’s quicker reading large files) is using the <code>readr</code> package which is part of the <code>tidyverse</code>. On a side note, if you haven’t already, <code>install.packages(tidyverse)</code>, it’s a must. It includes <code>readr</code> but a lot more goodies (<a rel="nofollow" href="https://blog.rstudio.com/2016/09/15/tidyverse-1-0-0/" target="_blank">read on</a>).<br>
Now, for some tools you get with <code>readr</code>:</p>
<pre><code>library(readr)
locale(&quot;he&quot;)</code></pre>
<pre>## </pre></div>