使用 gt opt_footnote_marks 得到令人困惑的结果
Using gt opt_footnote_marks get confusing result
我想使用opt_footnote_marks
根据另一列来标记一列,如下所示:
# packages
library("gt")
library("data.table")
# generate sample data.table
DT <- data.table(var1 = c(1:7),
p.value = c(0.7142599356, 0.0516123439, 0.0004532802, 0.1129728328, 0.3334458362, 0.3849460248, 0.0241492332))
# use gt to generate a gt_tib object
gt.table <- gt(data = DT)
# label var1 according to p.value
gt.table <-
tab_footnote(
data = gt.table,
footnote = "a",
locations = cells_body(
columns = var1,
rows = p.value >= 0.05 & p.value < 0.1)) %>%
tab_footnote(
data = .,
footnote = "b",
locations = cells_body(
columns = var1,
rows = p.value >= 0.01 & p.value < 0.05)) %>%
tab_footnote(
data = .,
footnote = "c",
locations = cells_body(
columns = var1,
rows = p.value < 0.01)) %>%
opt_footnote_marks(
data = .,
marks = c("*", "**", "***"))
# print table as html
gt.table
然而,结果非常有线。
如图所示: 首先,脚注“a”、“b”、“c”的顺序不正确。其次,p.value < 0.01 的行应标记为 ***。图片中只有**。谢谢!
在我通过调整脚注的顺序尝试了@Matt 的解决方案之后,
(我在调整的那一行加了注释)
# generate test data.table
DT <- data.table(var1 = c(1:7),
var1.p.value = c(0.7142599356, 0.0516123439, 0.0004532802, 0.1129728328, 0.3334458362, 0.3849460248, 0.0241492332),
var2 = c(8:14),
var2.p.value = c(0.0696197928, 0.0440874454, 0.1603414690, 0.0007604622, 0.2863551953, 0.1418785028, 0.3724698668))
# change data.table to gt_tib object
gt.table <- gt(data = DT)
# add footnote by looping
for(x in c("var1")){
gt.table <-
tab_footnote(
data = gt.table,
footnote = "a",
locations = cells_body(
columns = x,
rows = DT[[paste0(x, ".p.value")]] >= 0.05 & DT[[paste0(x, ".p.value")]] < 0.1)) %>%
tab_footnote(
data = .,
footnote = "b",
locations = cells_body(
columns = x,
rows = DT[[paste0(x, ".p.value")]] >= 0.01 & DT[[paste0(x, ".p.value")]] < 0.05)) %>%
tab_footnote(
data = .,
footnote = "c",
locations = cells_body(
columns = x,
rows = DT[[paste0(x, ".p.value")]] < 0.01)) %>%
opt_footnote_marks(
data = .,
marks = c("*", "***", "**")) # change mark order according to the present of the wanted row in the column.
}
gt.table
如result.2所示,var1和var1.p.value < 0.01被标记为***,var1和0.01 <= var1.p.value < 0.05被标记为* *。
但是当我尝试对两列做同样的工作时,标签和 p.value 之间的相关性再次发生变化。
result.3 如此处所示,p.value < 0.01 的 var1 或 var2 被标记为 ** 并且 0.01 <= p.value < 0.05 被标记为 ***。再次感谢!
脚注的显示顺序与它们首次出现在您的专栏中的顺序相同。第 2 行首先 a
,然后第 3 行 c
,第 7 行 b
。
如果您切换标签,它们将按您的意愿显示:
gt.table <-
tab_footnote(
data = gt.table,
footnote = "a",
locations = cells_body(
columns = var1,
rows = p.value >= 0.05 & p.value < 0.1)) %>%
tab_footnote(
data = .,
footnote = "c",
locations = cells_body(
columns = var1,
rows = p.value >= 0.01 & p.value < 0.05)) %>%
tab_footnote(
data = .,
footnote = "b",
locations = cells_body(
columns = var1,
rows = p.value < 0.01)) %>%
opt_footnote_marks(
data = .,
marks = c("*", "**", "***"))
我想使用opt_footnote_marks
根据另一列来标记一列,如下所示:
# packages
library("gt")
library("data.table")
# generate sample data.table
DT <- data.table(var1 = c(1:7),
p.value = c(0.7142599356, 0.0516123439, 0.0004532802, 0.1129728328, 0.3334458362, 0.3849460248, 0.0241492332))
# use gt to generate a gt_tib object
gt.table <- gt(data = DT)
# label var1 according to p.value
gt.table <-
tab_footnote(
data = gt.table,
footnote = "a",
locations = cells_body(
columns = var1,
rows = p.value >= 0.05 & p.value < 0.1)) %>%
tab_footnote(
data = .,
footnote = "b",
locations = cells_body(
columns = var1,
rows = p.value >= 0.01 & p.value < 0.05)) %>%
tab_footnote(
data = .,
footnote = "c",
locations = cells_body(
columns = var1,
rows = p.value < 0.01)) %>%
opt_footnote_marks(
data = .,
marks = c("*", "**", "***"))
# print table as html
gt.table
然而,结果非常有线。 如图所示: 首先,脚注“a”、“b”、“c”的顺序不正确。其次,p.value < 0.01 的行应标记为 ***。图片中只有**。谢谢!
在我通过调整脚注的顺序尝试了@Matt 的解决方案之后, (我在调整的那一行加了注释)
# generate test data.table
DT <- data.table(var1 = c(1:7),
var1.p.value = c(0.7142599356, 0.0516123439, 0.0004532802, 0.1129728328, 0.3334458362, 0.3849460248, 0.0241492332),
var2 = c(8:14),
var2.p.value = c(0.0696197928, 0.0440874454, 0.1603414690, 0.0007604622, 0.2863551953, 0.1418785028, 0.3724698668))
# change data.table to gt_tib object
gt.table <- gt(data = DT)
# add footnote by looping
for(x in c("var1")){
gt.table <-
tab_footnote(
data = gt.table,
footnote = "a",
locations = cells_body(
columns = x,
rows = DT[[paste0(x, ".p.value")]] >= 0.05 & DT[[paste0(x, ".p.value")]] < 0.1)) %>%
tab_footnote(
data = .,
footnote = "b",
locations = cells_body(
columns = x,
rows = DT[[paste0(x, ".p.value")]] >= 0.01 & DT[[paste0(x, ".p.value")]] < 0.05)) %>%
tab_footnote(
data = .,
footnote = "c",
locations = cells_body(
columns = x,
rows = DT[[paste0(x, ".p.value")]] < 0.01)) %>%
opt_footnote_marks(
data = .,
marks = c("*", "***", "**")) # change mark order according to the present of the wanted row in the column.
}
gt.table
如result.2所示,var1和var1.p.value < 0.01被标记为***,var1和0.01 <= var1.p.value < 0.05被标记为* *。 但是当我尝试对两列做同样的工作时,标签和 p.value 之间的相关性再次发生变化。 result.3 如此处所示,p.value < 0.01 的 var1 或 var2 被标记为 ** 并且 0.01 <= p.value < 0.05 被标记为 ***。再次感谢!
脚注的显示顺序与它们首次出现在您的专栏中的顺序相同。第 2 行首先 a
,然后第 3 行 c
,第 7 行 b
。
如果您切换标签,它们将按您的意愿显示:
gt.table <-
tab_footnote(
data = gt.table,
footnote = "a",
locations = cells_body(
columns = var1,
rows = p.value >= 0.05 & p.value < 0.1)) %>%
tab_footnote(
data = .,
footnote = "c",
locations = cells_body(
columns = var1,
rows = p.value >= 0.01 & p.value < 0.05)) %>%
tab_footnote(
data = .,
footnote = "b",
locations = cells_body(
columns = var1,
rows = p.value < 0.01)) %>%
opt_footnote_marks(
data = .,
marks = c("*", "**", "***"))