Search code examples
rnumbersletter

r- convert a word to numbers


I'm trying to convert a string "abcd" into a string of numbers in which each number will indicate the position of that specific letter in the alphabet, in this case this is needed: "1234". Is there a function to do this in R ?

I tried this:

str2int <- function(x) {
   match(x, letters[1:26])
}
str2int("abcd")

this didn't work since there are multiple characters in the string


Solution

  • If it is actually a string with no spaces etc which should be preseved and just a series of lowercase letters, you could use charToRaw and then convert to numeric and subtract 96.

    The reason you need to subtract 96 is because the as.numeric(chartoRaw(x)) gives the position of the characters on the ascii table, and a-z starts at position 97 on that table.

    test <- paste(letters, collapse = '')
    test
    # [1] "abcdefghijklmnopqrstuvwxyz"
    
    library(magrittr)
    
    test %>% 
      charToRaw %>% 
      as.numeric %>% 
      '-'(96) %>% 
      paste(collapse = '')
    # [1] "1234567891011121314151617181920212223242526"
    

    Edit: @akrun suggests a better method below. You can replace charToRaw %>% as.numeric with utf8toInt.