我想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.p.value < 0.01的var1被標記為***,var1的0.01 <= var1.p.value < 0.05被標記為**。但是當我嘗試對兩列做同樣的作業時,標簽和 p.value 之間的相關性再次發生了變化。 結果 3 如此處所示,p.value < 0.01 的 var1 或 var2 被標記為 **,0.01 <= p.value < 0.05 被標記為 ***。再次感謝!
uj5u.com熱心網友回復:
腳注的順序顯示為它們首次出現在您的列中。首先a是第 2 行,然后c是第 3行,然后b是第 7 行。
如果您切換標簽,它們將按您的意愿出現:
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("*", "**", "***"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/350443.html
標籤:r html-table 总吨 脚注
