本文介绍了R:有条件的突出有线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个像这样的矩阵:
set.seed(1234)x = rnorm(10,平均值= 0,sd = 1)y = rnorm(10,平均值= 0,sd = 1)z = rnorm(10,平均值= 0,sd = 1)数据= data.frame(rbind(x,y,z))
有什么方法可以在不指定列的情况下突出显示所有某些单元格(例如本例中的> 0)?
我的预期结果将是这样(仅突出显示正值):
更新
或者更改文本和背景颜色
---标题:无标题"输出:html_document:默认---```{r warning = FALSE,消息= FALSE,echo = FALSE}set.seed(1234)x = rnorm(10,平均值= 0,sd = 1)y = rnorm(10,平均值= 0,sd = 1)z = rnorm(10,平均值= 0,sd = 1)数据= data.frame(rbind(x,y,z))图书馆(针织)图书馆(kableExtra)图书馆(tidyverse)数据%&%;%mutate_all(〜cell_spec(.X,颜色= ifelse(.x< 0,白色",白色"),背景= ifelse(.x< 0,红色",黑色")))%>%kable(逸出= F)%>%kable_styling()```
Suppose I have a matrix like this:
set.seed(1234)
x = rnorm(10, mean=0, sd=1)
y = rnorm(10, mean=0, sd=1)
z = rnorm(10, mean=0, sd=1)
data=data.frame(rbind(x,y,z))
Is there any way I can highlight all certain cells (e.g. >0 in this case) without specifying the column?
My expected results would be like this (only positive values are highlighted):
In the real case, I have around 30 columns and it's exhausted if I try to column_spec each column one by one:
data %>%
column_spec(X1, color = "red") %>%
column_spec(X2, color = "red")
......
Thanks!
解决方案
You can do the following
---
title: "Untitled"
output:
html_document: default
---
```{r warning=FALSE, message=FALSE, echo=FALSE}
set.seed(1234)
x = rnorm(10, mean=0, sd=1)
y = rnorm(10, mean=0, sd=1)
z = rnorm(10, mean=0, sd=1)
data=data.frame(rbind(x,y,z))
library(knitr)
library(kableExtra)
library(tidyverse)
data %>%
mutate_all(~cell_spec(.x, color = ifelse(.x < 0, "red"," black"))) %>%
kable(escape = F) %>%
kable_styling()
```
This produces
Update
Or to change text and background colour
---
title: "Untitled"
output:
html_document: default
---
```{r warning=FALSE, message=FALSE, echo=FALSE}
set.seed(1234)
x = rnorm(10, mean=0, sd=1)
y = rnorm(10, mean=0, sd=1)
z = rnorm(10, mean=0, sd=1)
data=data.frame(rbind(x,y,z))
library(knitr)
library(kableExtra)
library(tidyverse)
data %>%
mutate_all(~cell_spec(
.x,
color = ifelse(.x < 0, "white", "white"),
background = ifelse(.x < 0, "red"," black"))) %>%
kable(escape = F) %>%
kable_styling()
```
这篇关于R:有条件的突出有线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!