Factor a numeric variable with a Greek expression in labels in R

Suppose the next data frame, I want to factor var and label numbers into Greek letters, from 1 to alpha, from 2 to beta, from 3 to gamma. But the following code does not work.

var<-c(1,1,2,2,3,3) df<-as.data.frame(var) df$var<-factor(df$var, levels=c(1,2,3), labels=c("1"=expression(alpha), "2"=expression(beta), "3"=expression(gamma))) 

Why is the final data frame not Greek letters, but simply textual expressions? Can someone help me with this? Thank you very much.

0
source share
2 answers

Does your language support these characters? Does '\ u03b1' produce an alpha character? If not, you will need to change the encoding. For instance.

 Sys.setlocale('LC_CTYPE', 'greek') 

Then replace your calls to expression with unicode strings for alpha, beta, etc.

 df$var<-factor(df$var, levels=c(1,2,3), labels=c("1"='\u03b1', "2"='\u03b2', "3"='\u03b3')) 

The use of expression valid only for graphs. If you really do not need the Greek letters in your factor, I suggest using the words alpha, beta, etc., until the time comes to build.

+2
source

df$var=factor(var,labels=c('alpha','beta','gamma'))

This creates a data block with 1,2,3 converted to alpha, beta, gamma. Hope this is what you were looking for.

0
source

Source: https://habr.com/ru/post/983829/


All Articles