dimpol dimpol - 13 days ago 5
R Question

How do I hard code the raw-character "83" in RStudio?

In R, there is something called raw-vector/raw-characters. The one from the "rawToChar" and "charToRaw" functions etc. If you put a raw-vector into the console, it gets printed. One such raw element might be printed as "83". Now I want to check whether for a given raw-character, it is equal to the raw-character that is printed as "83". So I want to put that raw-character hard-coded into my code to compare against. How do I do it?

The only way I found is using charToRaw("ƒ"). However this only works in the console. Putting ƒ into the source-code of an RStudio file is difficult. Just try:

print("ƒ")


That will give as output:

[1] "Æ’"

If the print-statement is in a compiled RStudio file (it only works as expected in the console).

Answer

The 83 is a raw value, i.e., a hexadecimal number.

You can use this:

intToUtf8(8 * 16 + 3) == "ƒ"
#[1] TRUE

or this

x <- rawToChar(as.raw(8*16+3))
Encoding(x) <- "latin1"
x == "ƒ"
#[1] TRUE

It's dangerous and not necessary to hard-code a UTF-8 character in source code.