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

时间:2015-03-15 17:20:29

标签: r knitr

我想知道如何设置矩阵的rownames,包括R中的希腊字母表达式。我使用"表达式",但它似乎不起作用。这是我的代码如下。

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="")) )

当我输入b.summary时,显示的rownames如下:

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

而不是乳胶表达。

我想要希腊字母的原因是我使用knitr来创建动态文档。我想直接显示此矩阵的结果,而不是使用\ Sexpr {}表达式手动键入矩阵的所有元素来创建表。 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
@

谢谢你的帮助!

2 个答案:

答案 0 :(得分:6)

在使用矩阵的约束下,这是我能做的最好的事情。 rownames不能是R expression - 被分类的对象。我正在构建一个名为&#39; greeks&#39;并使用名称从中提取Unicode值,然后使用参数回收来标记带有tau和sigma ^ 2的交替行。 (无法使用表达式意味着不能在矩阵行名称中使用子脚本。)

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

答案 1 :(得分:2)

调整@ 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

产生以下

          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

在我的特定用例中,我是KnitR的“kable”方差膨胀因子和s.d. mtcars数据集的通胀因素:

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 with Greek symbol and superscript

KnitR这需要xelatex和包含σ的字体,即Arial