gpt4 book ai didi

R:条件格式化具有百分比(%)值的列

转载 作者:行者123 更新时间:2023-12-05 03:27:24 24 4
gpt4 key购买 nike

我需要对 1 个特定列进行颜色条件格式化,将其格式化为百分比,并将文件导出为 .xlsx。请注意,我有 5 个数据框,我将使用这些数据框运行此规则代码,并将它们编译成 1 个工作簿,每个工作簿都在不同的工作表中。如果我在其中设置百分比格式,我就被困在我似乎无法设置条件规则的部分。反之亦然,如果我首先对其进行条件格式化,我不确定如何为该列设置百分比格式。请引用我下面的代码。

## Dataframe
cost_table <- read.table(text = "FRUIT COST SUPPLY_RATE
1 APPLE 15 0.026377
2 ORANGE 14 0.01122
3 KIWI 13 0.004122
5 BANANA 11 0.017452
6 AVOCADO 10 0.008324 " , header = TRUE)

## This is the line where I label the %. However if I do that, conditional formatting will not recognize it in the rule
cost_table$SUPPLY_RATE <- label_percent(accuracy = 0.01)(cost_table$SUPPLY_RATE)

## Creating workbook and sheet
Fruits_Table <- createWorkbook()
addWorksheet(Fruits_Table,"List 1")

writeData(Fruits_Table,"List 1",cost_table)


## Style color for conditional formatting
posStyle <- createStyle(fontColour = "#006100", bgFill = "#C6EFCE")
negStyle <- createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")

## If Supply rate is above 1.5%, it will be green, if it's equivalent or below, it will be red
conditionalFormatting(Fruits_Table, "List 1",
cols = 3,
rows = 2:6, rule = "C2> 0.015", style = posStyle
)

conditionalFormatting(Fruits_Table, "List 1",
cols = 3,
rows = 2:6, rule = "C2<= 0.015", style = negStyle
)

输出应该如下所示。

Output

关于边界信息

我正在查看的是为 c2:c6 应用外边界。 enter image description here

为了阐明我的目的,最终输出将如下所示。我还有一些其他代码来格式化标题和 A:B 列的边框。由于百分比样式,它影响了我的边界。

enter image description here

最佳答案

您不需要使用 scales 包中的 label_percent。您可以使用 style 然后使用 addStyle 函数将百分比格式连同颜色规则应用到工作簿。另一件事,我在 conditionalFormatting 的文档示例中发现您不需要在 rule 中指定列名(例如 C) > 如果您的规则仅适用于与另一列中的值无关的一列,则为参数。

这是我使用的代码:

Fruits_Table <- createWorkbook()
addWorksheet(Fruits_Table,"List 1")
writeData(Fruits_Table,"List 1",cost_table)
conditionalFormatting(Fruits_Table, "List 1",
cols = 3,
rows = 2:6, rule = "> 0.015", style = posStyle)
conditionalFormatting(Fruits_Table, "List 1",
cols = 3,
rows = 2:6, rule = "<= 0.015",
style = negStyle)
percent_style <- createStyle(numFmt = "PERCENTAGE")
addStyle(Fruits_Table,"List 1", style = ,percent_style, rows = 2:6, cols = 3)

我试过那个代码,它有效。

saveWorkbook(Fruits_Table, "my_fruits_table.xlsx", )

enter image description here

已更新以添加边界信息

如果你想创建边框和百分比格式,你可以使用 borderborderStyle 如下:


percent_border_style<- createStyle(numFmt = "PERCENTAGE",
border = "TopBottomLeftRight",
borderStyle = "medium" )

addStyle(Fruits_Table,"List 1",
style = ,percent_border_style,
rows = 2:6, cols = 3)

saveWorkbook(Fruits_Table, "borderline_fruits_table.xlsx", )

这是临界结果

enter image description here

如果您想为不同的单元格自定义不同的样式,正如您在评论中所解释的那样,您需要为特定样式createStyle,然后使用addStyle将该特定样式应用于特定单元格。因此,您需要为每种样式指定行和列。要保持百分比格式样式,您还需要为每个 addStyle 保持 numFmt

这是将外部边框应用于目标列的示例代码。该代码为三组单元格自定义边框:

top_side_line <- createStyle(numFmt = "PERCENTAGE", 
border = "TopLeftRight",
borderStyle = "medium")
side_line <- createStyle(numFmt = "PERCENTAGE",
border = "LeftRight",
borderStyle = "medium")
bottom_side_line <- createStyle(numFmt = "PERCENTAGE",
border = "BottomLeftRight",
borderStyle = "medium")
addStyle(Fruits_Table,"List 1",
style = top_side_line, rows = 2, cols = 3)
addStyle(Fruits_Table,"List 1",
style = side_line, rows = 3:5, cols = 3)
addStyle(Fruits_Table,"List 1",
style = bottom_side_line, rows = 6, cols = 3)
saveWorkbook(Fruits_Table, "newline_fruits_table.xlsx")

结果如下:

enter image description here

关于R:条件格式化具有百分比(%)值的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71506706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com