如何突出显示意外事件table中每列的最大值?
How to highlight the max of each column in a contingency table?
我试图在 R 中的意外事件 table 中突出显示每一列的最大值。
到目前为止我尝试了什么
tbl <- as.matrix(table(c(iris[5], data.frame(Petal.Width = cut(iris$Petal.Width, 3)))))
pos <- cbind(apply(tbl, 2, which.max), 1:dim(tbl)[2])
tbl <- addmargins(tbl)
# First attempt
tbl2 <- tbl
tbl2[pos] <- -tbl2[pos]
tbl2
## Petal.Width
## Species (0.0976,0.9] (0.9,1.7] (1.7,2.5] Sum
## setosa -50 0 0 50
## versicolor 0 -49 1 50
## virginica 0 5 -45 50
## Sum 50 54 46 150
# Second attempt
tbl2 <- tbl
tbl[pos] <- paste("*", tbl[pos])
print(tbl, justify = "right")
## Petal.Width
## Species (0.0976,0.9] (0.9,1.7] (1.7,2.5] Sum
## setosa * 50 0 0 50
## versicolor 0 * 49 1 50
## virginica 0 5 * 45 50
## Sum 50 54 46 150
如果我能找到将 "-"
更改为 "* "
但保留数字数据类型的可能性,那么第一次尝试会奏效。
如果我能找到将值(现在是字符串)与列名右对齐的可能性,则第二次尝试会奏效。
我的问题
你能给我一个解决方案,使这些尝试中的至少一个成功吗?或者一个完全不同的解决方案,我可以用它突出显示意外事件中每列的最大值 table?
谢谢
您可以尝试包 'formattable'。
install.packages("formattable")
library(formattable)
tbl_df <- as.data.frame.matrix(tbl)
tbl_df_sub <- tbl_df[1:3,]
t <- formattable(tbl_df_sub, list(
'(0.0976,0.9]'=color_tile("white", "green"),
'(0.9,1.7]'=color_tile("white", "green"),
'(1.7,2.5]'=color_tile("white", "green")
))
结果:
缺点是它只是 exportable 作为图像或 html 小部件,而不是实际的 table(尽管 table 在 R 中仍然可用) .
这可能不是您要找的,但我不知道您的目标是什么。如果您只是想在 R 控制台中突出显示 table 并且仍然使用 table 值本身,那么我的解决方案不是很好。难道你不能简单地使用你的第一个视觉解决方案,但保留一份原始的副本 table 用于你想用它做的任何其他事情吗?
由于R中不同打印方式有些不一致,解决方案比较意外:
tbl2 <- tbl
tbl[pos] <- paste("*", tbl[pos])
print(tbl, quote = FALSE, right = TRUE)
## Petal.Width
## Species (0.0976,0.7] (0.7,1.3] (1.3,1.9] (1.9,2.5] Sum
## setosa * 50 0 0 0 50
## versicolor 0 * 28 * 22 0 50
## virginica 0 0 21 * 29 50
## Sum 50 28 43 29 150
print.matrix
上的文档说:
print.matrix
and print.default
both print matrices, and each has at
least an optional argument that the other lacks. Also, both directly
dispatch into .Internal
code directly instead of relying on each
other. This mainly stems from historic compatibility and similar
reasons should be changed in the future.
我试图在 R 中的意外事件 table 中突出显示每一列的最大值。
到目前为止我尝试了什么
tbl <- as.matrix(table(c(iris[5], data.frame(Petal.Width = cut(iris$Petal.Width, 3)))))
pos <- cbind(apply(tbl, 2, which.max), 1:dim(tbl)[2])
tbl <- addmargins(tbl)
# First attempt
tbl2 <- tbl
tbl2[pos] <- -tbl2[pos]
tbl2
## Petal.Width
## Species (0.0976,0.9] (0.9,1.7] (1.7,2.5] Sum
## setosa -50 0 0 50
## versicolor 0 -49 1 50
## virginica 0 5 -45 50
## Sum 50 54 46 150
# Second attempt
tbl2 <- tbl
tbl[pos] <- paste("*", tbl[pos])
print(tbl, justify = "right")
## Petal.Width
## Species (0.0976,0.9] (0.9,1.7] (1.7,2.5] Sum
## setosa * 50 0 0 50
## versicolor 0 * 49 1 50
## virginica 0 5 * 45 50
## Sum 50 54 46 150
如果我能找到将 "-"
更改为 "* "
但保留数字数据类型的可能性,那么第一次尝试会奏效。
如果我能找到将值(现在是字符串)与列名右对齐的可能性,则第二次尝试会奏效。
我的问题
你能给我一个解决方案,使这些尝试中的至少一个成功吗?或者一个完全不同的解决方案,我可以用它突出显示意外事件中每列的最大值 table?
谢谢
您可以尝试包 'formattable'。
install.packages("formattable")
library(formattable)
tbl_df <- as.data.frame.matrix(tbl)
tbl_df_sub <- tbl_df[1:3,]
t <- formattable(tbl_df_sub, list(
'(0.0976,0.9]'=color_tile("white", "green"),
'(0.9,1.7]'=color_tile("white", "green"),
'(1.7,2.5]'=color_tile("white", "green")
))
结果:
缺点是它只是 exportable 作为图像或 html 小部件,而不是实际的 table(尽管 table 在 R 中仍然可用) .
这可能不是您要找的,但我不知道您的目标是什么。如果您只是想在 R 控制台中突出显示 table 并且仍然使用 table 值本身,那么我的解决方案不是很好。难道你不能简单地使用你的第一个视觉解决方案,但保留一份原始的副本 table 用于你想用它做的任何其他事情吗?
由于R中不同打印方式有些不一致,解决方案比较意外:
tbl2 <- tbl
tbl[pos] <- paste("*", tbl[pos])
print(tbl, quote = FALSE, right = TRUE)
## Petal.Width
## Species (0.0976,0.7] (0.7,1.3] (1.3,1.9] (1.9,2.5] Sum
## setosa * 50 0 0 0 50
## versicolor 0 * 28 * 22 0 50
## virginica 0 0 21 * 29 50
## Sum 50 28 43 29 150
print.matrix
上的文档说:
print.matrix
andprint.default
both print matrices, and each has at least an optional argument that the other lacks. Also, both directly dispatch into.Internal
code directly instead of relying on each other. This mainly stems from historic compatibility and similar reasons should be changed in the future.