r.user.05apr r.user.05apr - 1 year ago 80
R Question

Print ASCII-characters

I would like to create ASCII characters in loop based on an integer variable.
The result should be like this, but without the character vector:


for (i in 1:5) print(paste("ASCII:",v[i]))

If I start with

for (i in 65:69)

how do I continue?

Thanks&kind regards

Answer Source

Use intToUtf8():

for (i in 65:69) print(paste('ASCII:',intToUtf8(i)));
## [1] "ASCII: A"
## [1] "ASCII: B"
## [1] "ASCII: C"
## [1] "ASCII: D"
## [1] "ASCII: E"

This is not directly relevant to your question, but we can utilize the multiple argument of intToUtf8() to generate a character vector of the output in one line:

## [1] "ASCII: A" "ASCII: B" "ASCII: C" "ASCII: D" "ASCII: E"