Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:07:55+00:00 2026-05-23T20:07:55+00:00

This question is related to this previous one on how to replace accented strings

  • 0

This question is related to this previous one on how to replace accented strings like México with equivalent Latex code M\'{e}xico.

My problem here is slightly different. I am using a third party database with string variables with Spanish accents like above. However, the encoding appears odd since this is the behavior I get:

> grep("México",temp$dest_nom_ent)
integer(0)
> grep("Mexico",temp$dest_nom_ent)
integer(0)
> grep("xico",temp$dest_nom_ent)
[1] 18 19 20
> temp$dest_nom_ent[grep("xico",temp$dest_nom_ent)]
[2] "México" "México" "México"

where temp$dest_nom_ent is a variable with state names of México.

My question, then, is how to convert the string variable from the third party database into an encoding that standard R functions will recognize. Please note:

> Encoding(temp$dest_nom_ent)
 [1] "unknown" "unknown" "unknown" "unknown" "unknown" "unknown" "unknown"
 [8] "unknown" "unknown" "unknown" "unknown" "unknown" "unknown" "unknown"
[15] "unknown" "unknown" "unknown" "unknown" "unknown" "unknown" "unknown"
[22] "unknown" "unknown" "unknown" "unknown" "unknown" "unknown" "unknown"
[29] "unknown" "unknown" "unknown" "unknown" "unknown" "unknown" "unknown"
[36] "unknown" "unknown" "unknown" "unknown" "unknown" "unknown" "unknown"
[43] "unknown" "unknown"

For further info I am using Windows 7 64. Also note:

> charToRaw(temp$dest_nom_ent[18])
[1] 4d e9 78 69 63 6f

Which from this source coincides with Windows Spanish (Traditional Sort) locale.

M=4d
é=e9
x=78
i=69
c=63
o=6f

And also note:

> charToRaw("México")
[1] 4d c3 a9 78 69 63 6f
> Encoding("México")
[1] "latin1"

I have tried the following unsuccessfully (e.g. meaning grep("é",temp$dest_nom_ent) returns null vector):

Encoding(temp$dest_nom_ent)<-"latin1"
temp$dest_nom_ent <- iconv(temp$dest_nom_ent,"","latin1")
temp$dest_nom_ent  <- enc2utf8(temp$dest_nom_ent)
...

I checked supported character sets using iconvlist() and "WINDOWS-1252" is supported. The following, however, did not work:

> temp1 <- temp$dest_nom_ent[grep("xico",temp$dest_nom_ent)]
> temp1
[1] "México" "México" "México"
> Encoding(temp1)<-"WINDOWS-1252"
> temp1 <- iconv(temp1,"WINDOWS-1252","latin1")
> temp1
[1] "México" "México" "México"
> Encoding(temp1)
[1] "latin1" "latin1" "latin1"
> charToRaw(temp1[1])
[1] 4d e9 78 69 63 6f
> grep("é",temp1)
integer(0)

which compares to:

> temp2 <- c("México","México","México")
> temp2
[1] "México" "México" "México"
> Encoding(temp2)
[1] "latin1" "latin1" "latin1"
> charToRaw(temp2[1])
[1] 4d c3 a9 78 69 63 6f
> grep("é",temp2)
[1] 1 2 3)

Tried to find out the encoding by brute force like:

try(for(i in 1:length(iconvlist())){
    temp1 <- temp$dest_nom_ent[grep("xico",temp$dest_nom_ent)]
    Encoding(temp1)<-iconvlist()[i]
    temp1 <- iconv(temp1,iconvlist()[i],"latin1")
    print(grep("é",temp1))
    print(i)
        },silent=FALSE)

I am not familiar with try function but it still scapes at error instead of ignoring it so cannot check whole list:

...
[1] 17
integer(0)
[1] 18
integer(0)
[1] 19
integer(0)
[1] 20
Error in iconv(temp1, iconvlist()[i], "latin1") : 
  unsupported conversion from 'CP-GR' to 'latin1' in codepage 1252

Finally:

> Sys.getlocale()
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
> d<-c("México","México")
> for(i in 1:7){d1 <- str_sub(d[1],i,i); print(d1)}
[1] "M"
[1] "Ã"
[1] "©
[1] "x"
[1] "i"
[1] "c"
[1] "o"
> print(grep("é",d))
[1] 1 2

So it seems I will have to change the computer’s locale as suggested here. Also see here

PS: In case you wonder how with an English_United States.1252 locale I managed to type d<-c("México","México") the way is by setting up a secondary Spanish keyboard (traditional sort) using Control Panel > Clock, Language and Region > Region and Language > Keyboards and Languages > Change Keyboards and under installed services click add and navigate to Spanish traditional sort. Then under advanced key settings you can create a short-cut to switch keyboards. In my case Shit+Alt. So if I want to type ñ in English default locale, I do Shift+Alt followed by ; and then Shift+Alt to go back to English keyboard.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:07:56+00:00Added an answer on May 23, 2026 at 8:07 pm

    Well, I could not determine the coding of accents but the following accomplishes what I wanted. The trick was to convert to UTF-8, set the sub() option useBytes=TRUE and Joran’s suggestion to use sanitize.text.function=function(x){x} for xtable(). Here is the sample code. Easy to loop over all accented vowels:

    > temp1 <- unique(temp$dest_nom_ent)
    > temp1
     [1] "Aguascalientes"                  "Baja California"                
     [3] "Baja California Sur"             "Campeche"                       
     [5] "Coahuila de Zaragoza"            "Colima"                         
     [7] "Chiapas"                         "Guanajuato"                     
     [9] "Guerrero"                        "Hidalgo"                        
    [11] "Jalisco"                         "México"                         
    [13] "Michoacán de Ocampo"             "Morelos"                        
    [15] "Nayarit"                         "Oaxaca"                         
    [17] "Puebla"                          "Querétaro"                      
    [19] "Quintana Roo"                    "San Luis Potosí"                
    [21] "Sinaloa"                         "Tabasco"                        
    [23] "Tlaxcala"                        "Veracruz de Ignacio de la Llave"
    [25] "Zacatecas"                      
    > temp1 <- iconv(unique(temp1),"","UTF-8")
    > temp1
     [1] "Aguascalientes"                  "Baja California"                
     [3] "Baja California Sur"             "Campeche"                       
     [5] "Coahuila de Zaragoza"            "Colima"                         
     [7] "Chiapas"                         "Guanajuato"                     
     [9] "Guerrero"                        "Hidalgo"                        
    [11] "Jalisco"                         "México"                         
    [13] "Michoacán de Ocampo"             "Morelos"                        
    [15] "Nayarit"                         "Oaxaca"                         
    [17] "Puebla"                          "Querétaro"                      
    [19] "Quintana Roo"                    "San Luis Potosí"                
    [21] "Sinaloa"                         "Tabasco"                        
    [23] "Tlaxcala"                        "Veracruz de Ignacio de la Llave"
    [25] "Zacatecas"                      
    > Encoding(temp1)
     [1] "unknown" "unknown" "unknown" "unknown" "unknown" "unknown" "unknown"
     [8] "unknown" "unknown" "unknown" "unknown" "UTF-8"   "UTF-8"   "unknown"
    [15] "unknown" "unknown" "unknown" "UTF-8"   "unknown" "UTF-8"   "unknown"
    [22] "unknown" "unknown" "unknown" "unknown"
    > temp2 <- sub("é", "\\\\'{e}", temp1, useBytes = TRUE)
    > temp2 <- data.frame(temp2)
    > print(xtable(temp2),sanitize.text.function=function(x){x})
    % latex table generated in R 2.13.1 by xtable 1.5-6 package
    % Fri Jul 15 13:52:44 2011
    \begin{table}[ht]
    \begin{center}
    \begin{tabular}{rl}
      \hline
     & temp2 \\ 
      \hline
    1 & Aguascalientes \\ 
      2 & Baja California \\ 
      3 & Baja California Sur \\ 
      4 & Campeche \\ 
      5 & Coahuila de Zaragoza \\ 
      6 & Colima \\ 
      7 & Chiapas \\ 
      8 & Guanajuato \\ 
      9 & Guerrero \\ 
      10 & Hidalgo \\ 
      11 & Jalisco \\ 
      12 & M\'{e}xico \\ 
      13 & Michoacán de Ocampo \\ 
      14 & Morelos \\ 
      15 & Nayarit \\ 
      16 & Oaxaca \\ 
      17 & Puebla \\ 
      18 & Quer\'{e}taro \\ 
      19 & Quintana Roo \\ 
      20 & San Luis Potosí \\ 
      21 & Sinaloa \\ 
      22 & Tabasco \\ 
      23 & Tlaxcala \\ 
      24 & Veracruz de Ignacio de la Llave \\ 
      25 & Zacatecas \\ 
       \hline
    \end{tabular}
    \end{center}
    \end{table}
    

    As actually implemented in a loop:

    temp$dest_nom_ent <- iconv(
            temp$dest_nom_ent,"","UTF-8")
    temp$dest_nom_mun <- iconv(
            temp$dest_nom_mun,"","UTF-8")
    accents <-c("á","é","í","ó","ú")
    latex <-c("\\\\'{a}","\\\\'{e}","\\\\'{i}","\\\\'{o}","\\\\'{u}")
    for(i in 1:5){
        temp$dest_nom_ent<-sub(accents[i], latex[i], 
                temp$dest_nom_ent, useBytes = TRUE)
        temp$dest_nom_mun<-sub(accents[i], latex[i], 
                temp$dest_nom_ent, useBytes = TRUE)
    }
    capture.output(
            print(xtable(temp),sanitize.text.function=function(x){x}),
            file = "../paper/rTables.tex", append = FALSE)
    

    Still, the answer is incomplete in that I cannot explain what exactly was going on. Found it through trial and error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to a previous post of mine Here . Basically, I
This question is related with one of my earlier questions.. Previous Post In there
Thanks to Insin for answering a previous question related to this one. His answer
This question is related to my previous one: Running erlang shell as a daemon/service
This question is related to the one here: configurable dependencies with easy to mock
This is related question to my previous question but different one. After searching a
This question is directly related to my previous question ASP.NET AJAX Is it possible
This is kind of related to my previous question, but not really. I have
Ok, so - this is heavily related to my previous question Transforming an object
[This question is related to but not the same as this one .] My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.