问题
I am having trouble displaying Russian characters in the Rstudio console. I load an Excel file with Russian using the readxl package. The cyrillic displays properly in the dataframe. However, if I run a function that has an output that includes the variable names, the RStudio consoles displays symbols instead of the proper Cyrillic characters.
test.xlsx contains two columns - зависимая переменная (dependent variable - numeric) and независимая переменная (independent variable, factor).
зависимая_переменная независимая_переменная
5 а
6 б
8 в
8 а
7.5 б
6 в
5 а
4 б
3 в
2 а
5 б
My code:
Sys.setlocale(locale = "Russian")
install.packages("readxl")
require(readxl)
basetable <- readxl::read_excel('test.xlsx',sheet = 1)
View(basetable)
basetable$независимая_переменная <- as.factor(basetable$независимая_переменная)
str(basetable)
This is what I get for the output of the str function:
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 11 obs. of 2 variables:
$ çàâèñèìàÿ_ïåðåìåííàÿ : num 5 6 8 8 7.5 6 5 4 3 2 ...
$ íåçàâèñèìàÿ_ïåðåìåííàÿ: Factor w/ 3 levels "а","б","в": 1 2 3 1 2 3 1 2 3 1 ...
I want to have the variable names displayed properly in Russian because I will be building many models from this data. For reference, here is my sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Russian_Russia.1251 LC_CTYPE=Russian_Russia.1251
[3] LC_MONETARY=Russian_Russia.1251 LC_NUMERIC=C
[5] LC_TIME=Russian_Russia.1251
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] readxl_0.1.1 shiny_0.13.1 dplyr_0.4.3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.2 digest_0.6.9 assertthat_0.1 mime_0.4
[5] chron_2.3-47 R6_2.1.2 xtable_1.8-2 jsonlite_0.9.19
[9] DBI_0.3.1 magrittr_1.5 lazyeval_0.1.10 data.table_1.9.6
[13] tools_3.2.3 httpuv_1.3.3 parallel_3.2.3 htmltools_0.3
回答1:
Try to change dataframe colnames encoding to UTF-8.
Encoding(colnames(YOURDATAFRAME)) <- "UTF-8"
来源:https://stackoverflow.com/questions/36357425/displaying-cyrillic-in-rstudio-console