Search code examples
rimportcyrillic

Working with Cyrillic in R


Folks, one simple q, which really started to piss me off seriously. How do you work with Cyrillic? How do you prepare the xlsx or csv file for proper reading in R after importing? I tried numerous options from Google, saving in different formats, using encoding, opening in notepads, putting Sys.setlocale("LC_CTYPE", "ukrainian") - NOTHING WORKS for me.

I used

Sys.setlocale("LC_CTYPE", "ukrainian")

# Set working directory
setwd("C:~AO")

# Packages
library(xlsx)

# Read file
kyiv.dfrr <- read.xlsx2(file="Kyiv DFRR.xlsx", sheetIndex = 1, stringsAsFactors = FALSE)

or

kyiv.dfrr <- read.csv("Kyiv DFRR.csv", header = TRUE)

Result

                           Type Planned Planned...9.months.   Paid 
X..paid..planned..9.months. latitude
1                       ???????????  62,821              27,344 21,875                        
80.0 50.43494
2                       ???????????  40,000              20,000 12,000                        
60.0 50.45447
3                       ???????????  50,000              50,000 14,539                        
29.1 50.52310
4                      ????????????   9,490                 395      0                        
0.0 50.48074
5 ????????????????? (?????????????)   9,613               9,613  2,790                        
29.0 50.52318
6                     ?????????????   9,821               2,000      0                         
0.0 50.50171
longitude Kyiv.city.district                           MP
1  30.54683         ??????????   ????? ?????? ?????????????
2  30.50433     ??????????????  ???????? ???? ?????????????
3  30.45902        ??????????? ????????? ?????? ???????????
4  30.40263     ??????????????  ???????? ???? ?????????????
5  30.60095        ???????????     ?????? ???????? ????????
6  30.60764        ???????????     ?????? ???????? ????????

Simple and elegant solution exists, I still have hope! Thanks!


Solution

  • Seems that the problem is behind your R(Studio) locale (see the reproduction code below). I would 1) use readxl for reading XLSX files, 2) not mess up with locales (I had the same problem with reading CSV files some time before, and instead of just setting encoding = "UTF-8" changed locale -- and it ruined the RStudio output completely -- only update of RStudio helped). So I would try to restart or reinstall RStudio (especially if you can update it at the same time :).

    f <- "C:/Users/Alexey/Downloads/Kyiv DFRR.xlsx"
    
    df <- readxl::read_excel(f)
    Sys.setlocale("LC_CTYPE", "ukrainian")
    
    head(df)
    # A tibble: 6 x 10
                                                                                                                                 Object
                                                                                                                                  <chr>
    1                                                                   "друга нитка Головного міського каналізаційного колектора \r\n"
    2 "об'єкт по вул. Воровського, 2, - реставрація з пристосуванням під розміщення Державного спеціалізованого мистецького навчального
    3 велика окружна дорога на ділянці від просп. Маршала Рокоссовського до вул. Богатирської з будівництвом транспортної розв'язки на 
    4                                             "будівля бюджетної сфери - школа-дитячий садок N 173 \"Райдуга\" по вул. Блюхера, 3а"
    5                                            будівля бюджетної сфери - дошкільний навчальний заклад N 300 по вул. Радунській, 22/9а
    6                                                          стадіон із штучним покриттям по вул. Драйзера, 2б, у Деснянському районі
    # ... with 9 more variables: Type <chr>, Planned <dbl>, `Planned ( 9 months)` <dbl>, Paid <dbl>, `% paid/ planned (9
    #   months)` <dbl>, latitude <dbl>, longitude <dbl>, `Kyiv city district` <chr>, MP <chr>