{"id":1114,"date":"2018-06-15T14:12:10","date_gmt":"2018-06-15T14:12:10","guid":{"rendered":"http:\/\/www.xplortext.org\/?page_id=1114"},"modified":"2018-06-15T18:58:46","modified_gmt":"2018-06-15T18:58:46","slug":"non-suitable-characters-importing-texts","status":"publish","type":"page","link":"https:\/\/xplortext.unileon.es\/?page_id=1114","title":{"rendered":"Non suitable characters importing texts"},"content":{"rendered":"<p><strong><u>Some typical problems when importing texts in Windows, Linux and MAC<\/u><\/strong><\/p>\n<p><strong>1.- Description of the problem<\/strong><\/p>\n<p>Sometimes when you open a text file you see characters that are not suitable.<\/p>\n<p>For example, a file with UTF8 format in Mac may not be read correctly in Windows.<\/p>\n<p>It can be read with some editors (for example Notepad) without any problem because the editor can detect (and transform) different formats.<\/p>\n<p>To know the \"locale categories\" in R for our computer we can use Sys.getlocale function:<\/p>\n<p><span style=\"font-size: 10pt;\">Sys.getlocale(category = \"LC_ALL\")<\/span><\/p>\n<p>The answer is different for Mac, Lynux, Windows and locale codification (language). For example:<\/p>\n<p><span style=\"font-size: 10pt;\">Sys.getlocale()<\/span><\/p>\n<p><span style=\"font-size: 10pt;\">Sys.getlocale(\"LC_TIME\")<\/span><\/p>\n<p><span style=\"font-size: 10pt;\">## Not run:<\/span><\/p>\n<p><span style=\"font-size: 10pt;\">Sys.setlocale(\"LC_TIME\", \"de\")\u00a0\u00a0\u00a0\u00a0 # Solaris: details are OS-dependent<\/span><\/p>\n<p><span style=\"font-size: 10pt;\">Sys.setlocale(\"LC_TIME\", \"de_DE\")\u00a0 # Many Unix-alikes<\/span><\/p>\n<p><span style=\"font-size: 10pt;\">Sys.setlocale(\"LC_TIME\", \"de_DE.UTF-8\")\u00a0 # Linux, macOS, other Unix-alikes<\/span><\/p>\n<p><span style=\"font-size: 10pt;\">Sys.setlocale(\"LC_TIME\", \"de_DE.utf8\")\u00a0\u00a0 # some Linux versions<\/span><\/p>\n<p><span style=\"font-size: 10pt;\">Sys.setlocale(\"LC_TIME\", \"German\") # Windows<\/span><\/p>\n<p>To change the locale codification you can use Sys.setlocale function in R.<\/p>\n<p>My advice is not to change these options and adapt the text to the features of our computer and the operating system.<\/p>\n<p><strong>2.- How to prepare the text outside R<\/strong><\/p>\n<p>If input file has, for example, UFT8 Mac format it can be saved to UTF8 (Windows format) or ANSII format using Notepad editor.<\/p>\n<p>This new file can be read directly from R using read.csv2 function as the following examples:<\/p>\n<p>C2 &lt;- read.csv2(\"C:\/Xplortext\/ANSI.csv\",sep = \";\", header =TRUE, row.names = 1)<br \/>\nC3 &lt;- read.csv2(\"C:\/Xplortext\/UTF8.csv\",sep = \";\", header =TRUE, row.names = 1)<\/p>\n<p>You can save your dataframe in the format of your operating system using save function.<\/p>\n<p><strong>3.- How to prepare the text inside R<\/strong><\/p>\n<p>In some cases, it is not easy to clean the text of special characters, for example tweets may contain icons.<\/p>\n<p>In the Monica B\u00e9cue book:<\/p>\n<p><a href=\"http:\/\/www.pur-editions.fr\/detail.php?idOuv=4541\">http:\/\/www.pur-editions.fr\/detail.php?idOuv=4541<\/a><\/p>\n<p>you can download the scripts of chapters in:<\/p>\n<p><a href=\"http:\/\/www.xplortext.org\">http:\/\/www.xplortext.org<\/a><\/p>\n<p>The last example of this book is an application from twitter:<\/p>\n<p><a href=\"http:\/\/xplortext.unileon.es\/?page_id=1014\">http:\/\/xplortext.unileon.es\/?page_id=1014<\/a><\/p>\n<p>You can find two files in R data format depending on the operating system used:<\/p>\n<p>Windows format: <a href=\"http:\/\/www.xplortext.org\/Rdata\/copy_tweets_dot_df.RData\">http:\/\/www.xplortext.org\/Rdata\/copy_tweets_dot_df.RData<\/a><\/p>\n<p>Mac &amp; Linux format: <a href=\"http:\/\/www.xplortext.org\/Rdata\/mac\/copy_tweets_dot_df.RData\">http:\/\/www.xplortext.org\/Rdata\/mac\/copy_tweets_dot_df.RData<\/a><\/p>\n<p>This Windows file has latin-1 encoding format and Mac &amp; Linux has UTF8 format.<\/p>\n<p>It should be noted that Mac UTF format may not be read correctly in Windows.<\/p>\n<p>Frequently some special characters are read as blanks (or including invisible blanks). When the text is processed with TextData (Xplortext package) the words will be split in two.<\/p>\n<p>This type of errors appear frequently when using the tolower function from tm package.<\/p>\n<p>To convert characters we use iconv function from R base package.<\/p>\n<p>The names of encodings available are platform-dependent (Windows-Linux-Mac).<\/p>\n<p>All R platforms support \"\" (for the encoding of the current locale), \"latin1\" and \"UTF-8\".\u00a0 That is to say, when leaving the quotes blank, the coding of our computer is used.<\/p>\n<p><a href=\"https:\/\/developer.r-project.org\/Encodings_and_R.html\">https:\/\/developer.r-project.org\/Encodings_and_R.html<\/a><\/p>\n<p>The iconv() function from {base} package allows to change from one to other format:<br \/>\niconv(x, from = \"\", to = \"\", sub = NA, mark = TRUE, toRaw = FALSE)<\/p>\n<p>For example:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">1. Load the RData file from console, load(base) or loading a csv file. However, be aware of the fact that this file is not ready to be processed because it has characters that can not be read correctly by tm, Xplortext, quanteda or another packages.<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">2. If base$text is the text to convert, for example, from latin1 to UTF-8, create a temporal object:<\/span><br \/>\n<span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">A &lt;- iconv(base$text , 'latin1', 'UTF-8')<\/span><br \/>\n<span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">Using iconvlist() function you can obtain all the avaliable possibilities.<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">3. Check that the encoding in \"A\" object is correct.<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">4. Replace the text:<\/span><br \/>\n<span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">base$text &lt;- A<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">5. Save the new text:<\/span><br \/>\n<span style=\"font-size: 12pt; font-family: trebuchet ms, geneva, sans-serif;\">save(base, file=\u201dnewbase.RData\u201d)<\/span><\/p>\n<p><span style=\"font-family: trebuchet ms, geneva, sans-serif;\">Ther<\/span>e are some packages to help in this problem, for example:<\/p>\n<p><a href=\"https:\/\/cran.r-project.org\/web\/packages\/utf8\/utf8.pdf\">https:\/\/cran.r-project.org\/web\/packages\/utf8\/utf8.pdf<\/a><\/p>\n<p><a href=\"https:\/\/www.rdocumentation.org\/packages\/base\/versions\/3.5.0\/topics\/iconv\">https:\/\/www.rdocumentation.org\/packages\/base\/versions\/3.5.0\/topics\/iconv<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some typical problems when importing texts in Windows, Linux and MAC 1.- Description of the problem Sometimes when you open a text file you see characters that are not suitable. For example, a file&#46;&#46;&#46;<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1114","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=\/wp\/v2\/pages\/1114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1114"}],"version-history":[{"count":14,"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=\/wp\/v2\/pages\/1114\/revisions"}],"predecessor-version":[{"id":1132,"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=\/wp\/v2\/pages\/1114\/revisions\/1132"}],"wp:attachment":[{"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xplortext.unileon.es\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}