我正在尝试向gtsummary
表格的行标签添加脚注,但我不知道如何引用我想要的确切单元格。
预期输出
\n使用默认trial
数据集,我想在“Drug B”中添加一个脚注,内容为“ie placebo”:
特征 | N = 200\xc2\xb9 |
---|---|
化疗治疗 | |
__ 药物A | 98 (49%) |
__ 药物 B \xc2\xb2 | 102 (51%) |
\xc2\xb9 n (%) | |
\xc2\xb2 即安慰剂 |
我尝试转换为gt
表格,然后使用tab_footnote()
and cells_stub()
,但我不知道如何使用row =
来引用我想要的特定行标签。
不幸的是,文档示例cells_stub()
仅使用其默认locations = everything()
参数值。
library(gtsummary)\nlibrary(gt)\n\ntrial["trt"] |>\n tbl_summary() |>\n as_gt() |>\n tab_footnote(footnote = "i.e. placebo",\n # Line below doesn\'t work\n locations = cells_stub(rows = "Drug B"))\n
Run Code Online (Sandbox Code Playgroud)\n 我可以使用变量来表示 中的列名称dplyr
mutate
,只要它位于计算部分的右侧即可。这工作正常:
library(dplyr)
var <- "mass"
x <- starwars %>%
mutate(height = height * 2,
mass = get(var) * 2)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用变量来表示新的列名称(即在左侧),则会抛出错误。例如,这会导致错误:
var <- "mass"
x <- starwars %>%
mutate(height = height * 2,
get(var) = get(var) * 2)
Run Code Online (Sandbox Code Playgroud)
如何在 中使用变量作为列名mutate
?
我想通过为比例尺底部和顶部的值添加手动标签名称(低和高)来更改连续颜色图例。我怎样才能做到这一点?
x <- 1:100
y <- runif(100) * 100
tib <- tibble(x, y)
ggplot(tib, aes(x = x, y = y, color = y)) +
geom_point() +
binned_scale(aesthetics = "color",
scale_name = "stepsn",
palette = function(x) c("red", "yellow", "green", "yellow", "red"),
breaks = c(5, 10, 25, 60),
limits = c(0, 100),
show.limits = TRUE,
guide = "colorsteps")
Run Code Online (Sandbox Code Playgroud)
预期输出:
我尝试labels = c("Low", 5, 10, 25, 60, "High")
在脚本中添加,但它显示了此错误:
Error in f():
! Breaks and labels are different lengths
Run rlang::last_error() to …
Run Code Online (Sandbox Code Playgroud) 当我%in%
在重新编码分类变量的条件下使用时,遇到了意外的输出。
当左侧向量的元素为 时NA
,条件计算结果为FALSE
,而我期望它是NA
。
预期的行为是更详细的语句,其中两个==
条件由|
dt <- data.frame(colour = c("red", "orange", "blue", NA))
# Expected
dt$is_warm1 <- ifelse(dt$colour == "red" | dt$colour == "orange", TRUE, FALSE)
# Unexpected
dt$is_warm2 <- ifelse(dt$colour %in% c("red", "orange"), TRUE, FALSE)
dt
Run Code Online (Sandbox Code Playgroud)
#> colour is_warm1 is_warm2
#> 1 red TRUE TRUE
#> 2 orange TRUE TRUE
#> 3 blue FALSE FALSE
#> 4 <NA> NA FALSE
Run Code Online (Sandbox Code Playgroud)
这在重新编码分类变量时非常没有帮助,因为它会默默地填充缺失值。为什么会发生这种情况?是否有任何不涉及列出所有==
条件的替代方案?(想象一下colour
包含三十个可能的级别)。