Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return the character associated with the specified Ascii code in R

Tags:

r

ascii

character

Good afternoon,

I'm trying to create a cartesian product in R with the letters of the alphabet.

What I'm actually trying is this:

First I create a matrix with the letters

a <- as.matrix(seq(97,122,by=1))

Then I create a data frame with 2 columns with all the combinations

b <- expand.grid(a, a)

Finally I combine the 2 columns

apply(b,1,paste,collapse=" ")

The problem I have is that I can't find a way to transform those "decimals" to its Ascii character.

I have tried several things like rawToChar and gsub unsuccessfully.

Can somebody point me in the right direction?

Thanks

like image 514
Diego Avatar asked Mar 02 '13 20:03

Diego


People also ask

How do I write Ascii code in r?

To get the letter, character, sign or symbol "R" : ( Capital letter R ) on computers with Windows operating system: 1) Press the "Alt" key on your keyboard, and do not let go. 2) While keep press "Alt", on your keyboard type the number "82", which is the number of the letter or symbol "R" in ASCII table.

What is the ascii value of '\ r in C?

In short \r has ASCII value 13 (CR) and \n has ASCII value 10 (LF).

What is charToRaw?

charToRaw converts a length-one character string to raw bytes. It does so without taking into account any declared encoding (see Encoding ). rawToChar converts raw bytes either to a single character string or a character vector of single bytes. (Note that a single character string could contain embedded nuls.)


1 Answers

A very easy way to return a character based on its ASCII code is the function intToUtf8. It also works for vectors including multiple integers and returns the corresponding characters as one string.

vec <- 97:122
intToUtf8(vec)
# [1] "abcdefghijklmnopqrstuvwxyz"

intToUtf8(65)
# [1] "A"
like image 197
Sven Hohenstein Avatar answered Oct 01 '22 11:10

Sven Hohenstein