问题描述
假设以下数据框架,我想将var和标签号分解为希腊字母,从1到alpha,2到beta,3到gamma。但以下代码不起作用。 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=表达式(beta),
3=表达式(gamma))
为什么最终的数据框不是希腊字母,而只是文字表达?有人可以帮我吗非常感谢。
您的区域设置是否支持这些字符? '\\\α'打印字母字符?如果没有,您需要更改编码。例如,
Sys.setlocale('LC_CTYPE','greek')
/ pre>
然后使用alpha,beta等unicode字符串将您的调用替换为
表达式
。df $ var< -factor(df $ var,levels = c(1,2,3),
labels = c (1='\\\α',
2='\\\β',
3='\\\γ'))
您使用
表达式
的方式仅适用于绘图。除非你真的需要在你的系数中加上希腊字母,否则我建议您使用alpha,beta等字样,直到画面为止。Suppose the following data frame, I want to factorized var, and label numbers to Greek letters, from 1 to alpha, 2 to beta, 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 the final data frame is not greek letters but just text expressions? Can anyone help me on this? Thanks a lot.
解决方案Does your locale support these characters? Does '\u03b1' print an alpha character? If not, you'll need to change your encoding. E.g.,
Sys.setlocale('LC_CTYPE', 'greek')
Then replace your calls to
expression
with the 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 way you're using
expression
is only valid for plots. Unless you really need to have Greek letters in your factor, I suggest using the words 'alpha', 'beta', etc. until it's time to plot.这篇关于将R中的标签中的希腊表达式的数字变量进行因式分解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!