简体   繁体   English

如何将希腊字母显示为矩阵的rownames

[英]How to show greek letters as the rownames of a matrix

I am wondering how to set the rownames of a matrix including greek letters expressions in R. I use "expression", but it seems not working. 我想知道如何设置矩阵的rownames,包括R中的希腊字母表达式。我使用“表达式”,但它似乎不起作用。 Here is my code below. 这是我的代码如下。

b.summary = matrix(0, 8, 6)
colnames(b.summary)= c("Min.", "1st Qu.", "Median", "Mean","3rd Qu", "Max." )
rownames(b.summary)= c(expression(paste(tau, "=1", sep="")),expression(paste(sigma^2, "=1", sep="")), expression(paste(tau, "=5",sep="")), expression(paste(sigma^2, "=0.2",sep="")), expression(paste(tau, "=16", sep="")), expression(paste(sigma^2, "=0.0625",sep="")), expression(paste(tau, "1/2.25", sep="")),expression( paste(sigma^2, "=2.25", sep="")) )

When I type b.summary, the rownames shown is below: 当我输入b.summary时,显示的rownames如下:

paste(tau, "=1", sep = "")

instead of the latex expression. 而不是乳胶表达。

The reason why I want the greek letters is that I am using knitr to create a dynamic document. 我想要希腊字母的原因是我使用knitr来创建动态文档。 I want to show the result of this matrix directly instead of creating a table manually typing all the elements of the matrix using \\Sexpr{} expression. 我想直接显示此矩阵的结果,而不是使用\\ Sexpr {}表达式手动键入矩阵的所有元素来创建表。 The complete code chunk in knitr is knitr中的完整代码块是

<<coverage.b.summary, eval=TRUE, echo=FALSE>>=
 b.summary = matrix(runif(48), 8, 6)
 colnames(b.summary)= c("Min.", "1st Qu.", "Median", "Mean","3rd Qu", "Max." )
rownames(b.summary)= labels(expression(paste(tau, "=1",     sep="")),expression(paste(sigma^2, "=1", sep="")), expression(paste(tau, "=5",sep="")), expression(paste(sigma^2, "=0.2",sep="")), expression(paste(tau, "=16", sep="")), expression(paste(sigma^2, "=0.0625",sep="")), expression(paste(tau, "=1/2.25", sep="")),expression( paste(sigma^2, "=2.25", sep="")) )
b.summary
@

Thank you advance for your help! 谢谢你的帮助!

This is the best I can do under the constraint of using a matrix. 这是我在使用矩阵的约束下可以做的最好的事情。 The rownames cannot be R expression -classed objects. rownames不能是R expression对象。 I am building a named vector called 'greeks' and pulling Unicode values from it using the names, and then using argument recycling to label alternating rows with tau and sigma^2. 我正在构建一个名为'greeks'的命名向量,并使用名称从中提取Unicode值,然后使用参数回收来标记交替行与tau和sigma ^ 2。 (The inability to use expressions means cannot have sub-scripting in matrix row names.) (无法使用表达式意味着不能在矩阵行名称中使用子脚本。)

greeks=c(alpha='\u03b1', tau='\u03c4', sigma='\u03c3',
                         beta='\u03b2',
                         gamma='\u03b3')

b.summary = matrix(0, 8, 6)
colnames(b.summary)= c("Min.", "1st Qu.", "Median", "Mean","3rd Qu", "Max." )
rownames(b.summary)= paste0(c( greeks['tau'], paste0(greeks['sigma'],"^2") ), 
                               c("=1","=1", "=5", "=0.2",
                                 "=16",  "=0.0625", "=2.25", "=2.25") )

> b.summary
           Min. 1st Qu. Median Mean 3rd Qu Max.
τ=1           0       0      0    0      0    0
σ^2=1         0       0      0    0      0    0
τ=5           0       0      0    0      0    0
σ^2=0.2       0       0      0    0      0    0
τ=16          0       0      0    0      0    0
σ^2=0.0625    0       0      0    0      0    0
τ=2.25        0       0      0    0      0    0
σ^2=2.25      0       0      0    0      0    0

Tweaking @42- solution (this should be a comment, but an answer has better code formatting): 调整@ 42-解决方案(这应该是一个评论,但答案有更好的代码格式):

greeks = c(alpha='\u03b1', tau='\u03c4', sigma='\u03c3', sigmaSq='\u03c3\u00B2', beta='\u03b2', gamma='\u03b3')
b.summary = matrix(0, 8, 6)
colnames(b.summary) = c("Min.", "1st Qu.", "Median", "Mean","3rd Qu", "Max.")
p1 = c(greeks['tau'], greeks['sigmaSq'])
p2 = c("1","1", "5", "0.2", "16",  "0.0625", "2.25", "2.25")
rownames(b.summary) = paste(p1, p2, sep="=")
b.summary

produces the following 产生以下

          Min. 1st Qu. Median Mean 3rd Qu Max.
τ=1          0       0      0    0      0    0
σ²=1         0       0      0    0      0    0
τ=5          0       0      0    0      0    0
σ²=0.2       0       0      0    0      0    0
τ=16         0       0      0    0      0    0
σ²=0.0625    0       0      0    0      0    0
τ=2.25       0       0      0    0      0    0
σ²=2.25      0       0      0    0      0    0

In my particular use case I am KnitR'ing a 'kable' of variance inflation factors and sd inflation factors for the mtcars dataset: 在我的特定用例中,我是针对mtcars数据集的变量通胀因子和sd通胀因素的'kable':

cars.vif = rbind(
    t(vif(cars.model)[,1]),
    t(sqrt(vif(cars.model))[,1])
)
rownames(cars.vif) = c("\u03c3", "\u03c3\u00B2")
kable(cars.vif)

Rownames与希腊符号和上标

KnitR'ing this requires xelatex and font that contains σ ie Arial KnitR'ing这需要xelatex和包含σ即Arial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM