有条件地格式化数据框中包含行最大值的每个单元格 - R Markdown PDF
我正在使用 R Markdown 创建一个包含一些表格的 PDF 文档。
这是
output:
pdf_document:
toc: yes
require("pacman")
p_load(tidyverse, reshape, reshape2, knitr, kableExtra, tinytex, scales, pander, janitor, gridExtra)
segment<- c('seg1', 'seg1', 'seg2', 'seg2', 'seg3', 'seg3')
subSegment<- c('subseg1', 'subseg2', 'subseg1', 'subseg2', 'subseg1', 'subseg2')
var.1<- c(100, 20, 30, 50, 40, 40)
var.2<- c(200, 30, 30, 70, 30, 140)
var.3<- c(50, 50, 40, 20, 30, 40)
var.4<- c(60, 50, 35, 53, 42, 20)
df<- data.frame(segment, subSegment, var.1, var.2, var.3, var.4)
df%>%adorn_totals('row')
df.2<-df
df.2[c(3:ncol(df.2))] = sapply(df.2[c(3:ncol(df.2))], function(x) scales::percent(x, accuracy = 0.1))
df.2 %>%
kable(format = "latex", booktabs = TRUE, caption = "Title" ,align = "c") %>%
kable_styling(latex_options = c("HOLD_position", "repeat_header", "scale_down"), font_size = 6) %>%
group_rows(index = table(fct_inorder(df$`segment`)), italic = F, bold = F, underline = T, latex_gap_space = "1em", background = "#f2f2f2")%>%
column_spec(1, monospace = T, color = "white") %>%
row_spec(nrow(df), bold = T)
我知道如何使用 column_spec 有条件地更改列的格式的示例必要数据,例如:
column_spec(ncol(df), color = ifelse(df$var.4 > 50, "green","red"))
我想为每行中具有最大值的单元格提供格式“bold = T”...
是有一个简单的方法吗?这只是一个例子,但我真正的 df 有数百行和超过 20 列,因此手动执行确实不是一个选择...我发现了一些类似的问题,但主要是针对 HTML knitting 解决这个问题。
我尝试过类似的事情:
df[3:ncol(df)] <- lapply(df[3:ncol(df)], function(x) {
cell_spec(x, font_size = spec_font_size(x))
})
但这不起作用,即使它起作用,结果也不是我真正想要的,因为它们改变了大小而不是使最大粗体。
简短描述:突出显示数据框中每行的最大值 编辑:编辑以添加数字具有特殊格式
I am using R Markdown to create a PDF document with a few tables.
Here is the example necessary data
output:
pdf_document:
toc: yes
require("pacman")
p_load(tidyverse, reshape, reshape2, knitr, kableExtra, tinytex, scales, pander, janitor, gridExtra)
segment<- c('seg1', 'seg1', 'seg2', 'seg2', 'seg3', 'seg3')
subSegment<- c('subseg1', 'subseg2', 'subseg1', 'subseg2', 'subseg1', 'subseg2')
var.1<- c(100, 20, 30, 50, 40, 40)
var.2<- c(200, 30, 30, 70, 30, 140)
var.3<- c(50, 50, 40, 20, 30, 40)
var.4<- c(60, 50, 35, 53, 42, 20)
df<- data.frame(segment, subSegment, var.1, var.2, var.3, var.4)
df%>%adorn_totals('row')
df.2<-df
df.2[c(3:ncol(df.2))] = sapply(df.2[c(3:ncol(df.2))], function(x) scales::percent(x, accuracy = 0.1))
df.2 %>%
kable(format = "latex", booktabs = TRUE, caption = "Title" ,align = "c") %>%
kable_styling(latex_options = c("HOLD_position", "repeat_header", "scale_down"), font_size = 6) %>%
group_rows(index = table(fct_inorder(dfI am using R Markdown to create a PDF document with a few tables.
Here is the example necessary data
segment`)), italic = F, bold = F, underline = T, latex_gap_space = "1em", background = "#f2f2f2")%>%
column_spec(1, monospace = T, color = "white") %>%
row_spec(nrow(df), bold = T)
I know how to use column_spec to conditionally change the format of a column, for example:
column_spec(ncol(df), color = ifelse(df$var.4 > 50, "green","red"))
I would like to give the format 'bold = T' to the cell with the maximum value in each row...
Is there a simple way to do so? this is just an example but my real df has hundreds of rows and over 20 columns so doing it manually is really not an option... I found some similar questions but mostly solving this for HTML knitting.
I trid something like:
df[3:ncol(df)] <- lapply(df[3:ncol(df)], function(x) {
cell_spec(x, font_size = spec_font_size(x))
})
but that didn't work and even if it did the results are not really what I am looking for as they change the size instead of making the maximum bold.
Short description: highlight the maximum value of each row in a data frame
Edited: edited to add that numbers have a special format
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 dplyr::mutate(across... 和 max(c_across... 是一种方法:
导致此 pdf 输出:
Using dplyr::mutate(across... and max(c_across... is one way:
Which results in this pdf output: