R到乳胶-自动为数字着色 [英] R to latex - Coloring numbers automatically

查看:82
本文介绍了R到乳胶-自动为数字着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(长)R矩阵。例如:

I have a (long) R matrix. eg:

matrix <- matrix(rexp(200, rate=.01), ncol=4)

我想找到一种上色的方法,例如,每列的重要数字增加15%,之前进行乳胶提取,如下所示:

I would like to find a way to colour, for instance, the 15% more important numbers of each column, BEFORE doing the latex extraction as follow:

print(xtable(matrix, align = c("r","r","r","r","r")),
type = "latex",
floating = FALSE,
tabular.environment = "longtable")

有什么想法吗?

推荐答案

我终于找到了一个肮脏的解决方案

I finally found a dirty solution

matrix <- as.data.frame(matrix(rexp(200, rate=.01), ncol=4))

设置循环

for(i in 1:length(matrix[1,])) {
quant  <- quantile(matrix[,i], prob = 0.85, na.rm = TRUE)   
   for(j in 1:length(matrix[,1])) {         
       if(as.numeric(matrix[j,i]) > quant) {
       matrix[j,i] <- paste("\\cellcolor{red!25}", matrix[j,i], sep="", collapse = NULL)} 
       else {}  
} } # close both loops

然后在乳胶中打印结果

print(xtable(matrix), 
      type = "latex",
      sanitize.text.function = identity)

它给出了可接受的结果。重要的是设置:在 j循环之前设置 quant-分位数。如果不是,则在此 j循环中进行的更改会将matrix [,i]更改为字符向量,因此无法重新计算分位数。

It give an acceptable result. It is important to set: "quant <- quantile" before the "j" loop. If not the change made during this "j" loop change matrix[,i] into a character vector and it is then impossible to re-compute the quantile.

不要忘记打印中的 sanitize.text.function = identity。

Don't forget "sanitize.text.function = identity" in the print.

这篇关于R到乳胶-自动为数字着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆