Search code examples
rcharacter-encodingspecial-characterskableextra

How to print the sign "±" in a string in R (to place this character in a table like `kableextra`)


I want to print a plus minus sign of the form ± into a string in R, so that I can place that string a table with kableextra

Here is the structure of the string :

x = paste0("first_string", 2, "±", 3, "second_string", collapse = "")

However I get the following output:

"first_string2\302\2613second_string"

what should I do to have the output:

"first_string2±3second_string"

I found that \302\261 is the encoding for ± in Octal Escape Sequence according to this website or this website

EDIT Here is the output of sessionInfo():

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] C

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2.2   rms_5.1-2        SparseM_1.77     Hmisc_4.1-1      Formula_1.2-3   
 [6] lattice_0.20-35  blme_1.0-4       emmeans_1.2.4    lme4_1.1-18-1    Matrix_1.2-14   
[11] multcomp_1.4-8   TH.data_1.0-8    MASS_7.3-49      mvtnorm_1.0-8    survminer_0.4.3 
[16] survival_2.41-3  latex2exp_0.4.0  magick_1.9       cowplot_0.9.3    ggpubr_0.1.7    
[21] magrittr_1.5     gridExtra_2.3    rcompanion_2.0.0 ggsignif_0.4.0   ggplot2_3.1.0   
[26] chron_2.3-52     lubridate_1.7.4  dplyr_0.7.5      kableExtra_0.9.0 knitr_1.20      
[31] broom_0.4.5     

loaded via a namespace (and not attached):
 [1] minqa_1.2.4         colorspace_1.3-2    class_7.3-14        modeltools_0.2-22  
 [5] rprojroot_1.3-2     estimability_1.3    htmlTable_1.12      base64enc_0.1-3    
 [9] rstudioapi_0.7      MatrixModels_0.4-1  manipulate_1.0.1    coin_1.2-2         
[13] xml2_1.2.0          codetools_0.2-15    mnormt_1.5-5        nloptr_1.0.4       
[17] km.ci_0.5-2         cluster_2.0.7-1     readr_1.1.1         compiler_3.5.0     
[21] httr_1.3.1          backports_1.1.2     assertthat_0.2.0    lazyeval_0.2.1     
[25] quantreg_5.36       acepack_1.4.1       htmltools_0.3.6     tools_3.5.0        
[29] coda_0.19-1         gtable_0.2.0        glue_1.3.0          reshape2_1.4.3     
[33] Rcpp_1.0.0          nlme_3.1-137        psych_1.8.4         lmtest_0.9-36      
[37] stringr_1.3.1       rvest_0.3.2         polspline_1.1.13    zoo_1.8-2          
[41] scales_1.0.0        hms_0.4.2           parallel_3.5.0      sandwich_2.4-0     
[45] expm_0.999-2        RColorBrewer_1.1-2  yaml_2.1.19         BSDA_1.2.0         
[49] KMsurv_0.1-5        EMT_1.1             rpart_4.1-13        latticeExtra_0.6-28
[53] stringi_1.2.4       nortest_1.0-4       e1071_1.6-8         checkmate_1.8.5    
[57] boot_1.3-20         rlang_0.3.0.1       pkgconfig_2.0.1     evaluate_0.10.1    
[61] purrr_0.2.5         bindr_0.1.1         labeling_0.3        htmlwidgets_1.2    
[65] cmprsk_2.2-7        tidyselect_0.2.4    plyr_1.8.4          R6_2.3.0           
[69] DescTools_0.99.25   multcompView_0.1-7  pillar_1.2.3        foreign_0.8-70     
[73] withr_2.1.2         nnet_7.3-12         tibble_1.4.2        survMisc_0.5.5     
[77] rmarkdown_1.10      grid_3.5.0          data.table_1.11.4   digest_0.6.18      
[81] xtable_1.8-2        tidyr_0.8.1         stats4_3.5.0        munsell_0.5.0      
[85] viridisLite_0.3.0  

Solution

  • Locale settings control character sets for input and output. You have

    locale:
    [1] C
    

    but if you want to print non-ASCII characters such as ±, you'll need something such as UTF-8. One example of how to accomplish this (which seems to have solved your issue per the comments) is

    Sys.setlocale("LC_ALL","en_US.UTF-8")
    paste0("first_string", 2, "±", 3, "second_string", collapse = "")
    

    which should output

    [1] "first_string2±3second_string"
    

    For further information, I'd check out the following sources as a starting point: