Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

Print unicode character string in R

I entered a text string in .csv file , which includes unicode symbols as: U00B5 g/dL. In .csv file as well as read in R data frame:

enter image description here

test=read.csv("test.csv")

enter image description here

U00B5 would produce the micro sign- μ. R read it into data file as it is (U00B5). However when I print the string it shows as \U00B5 g/dL.
Alternatively, manually entering the code works fine.

varname <- c("a", "b", "c")
labels <- c("A U00B5 g/dL", "B U00B5 g/dL", "C U00B5 g/dL")
df <- data.frame(varname, labels)
test <- data.frame(varname, labels)
test
#  varname   labels
#  1       a A μ g/dL
#  2       b B μ g/dL
#  3       c C μ g/dL

I wonder how could I get rid of the escape sign in this case and have it print out the symbol. Or, if there another way to print out the symbol in R.

Thank you very much for this help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, first understand that certain characters in R must be escaped if they are outside the standard ASCII-characters. Typically this is done with a "" character. That's why you need to escape this character when you write a string in R:

a <- "" # error
a <- "" # ok.

The "U" is a special indicator for unicode escaping. Note that there are no slashes or U's in the string itself when you use this escaping. It is just a shortcut to a specific character. Note:

a <- "U00B5"
cat(a)
# μ
grep("U",a)
# integer(0)
nchar(a)
# [1] 1

This is very different than the string

a <- "\U00B5"
cat(a)
# U00B5
grep("U",a)
# [1] 1
nchar(a)
# [1] 6

Normally when you import a text file, you would encode non-ASCII character in whatever encoding is used by the file (UTF-8, or Latin-1 are the most common). They have special bytes to represent these characters. It's not "normal" for a text file to have an ASCII escape sequence for unicode characters. This is why R doesn't attempt to convert "U00B5" to a unicode character because it assumes that if you had wanted a unicode character, you would have just used it directly.

The easiest way to re-interpet your ASCII character values would be to use the stringi package. For example

library(stringi)
a <- "\U00B5"
stri_unescape_unicode(gsub("\U","\u",a, fixed=TRUE))

(the only catch is that we needed to convert "U" to the more common "u" so the function properly recognized the escape). You can do this to your imported data with

test$label <- stri_unescape_unicode(gsub("\U","\u",test$label, fixed=TRUE))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...