小编And*_*a M的帖子

将脚注添加到 gsummary 表中的单行标签

我正在尝试向gtsummary表格的行标签添加脚注,但我不知道如何引用我想要的确切单元格。

\n

预期输出

\n

使用默认trial数据集,我想在“Drug B”中添加一个脚注,内容为“ie placebo”:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n
特征N = 200\xc2\xb9
化疗治疗
__ 药物A98 (49%)
__ 药物 B \xc2\xb2102 (51%)
\xc2\xb9 n (%)
\xc2\xb2 即安慰剂
\n
\n

我尝试转换为gt表格,然后使用tab_footnote()and cells_stub(),但我不知道如何使用row = 来引用我想要的特定行标签。

\n

不幸的是,文档示例cells_stub()仅使用其默认locations = everything()参数值。

\n
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

r footnotes gtsummary gt

6
推荐指数
1
解决办法
2766
查看次数

在 dplyr mutate 中,如何使用变量来分配新的列名?

我可以使用变量来表示 中的列名称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

r dplyr

3
推荐指数
1
解决办法
772
查看次数

使用 ggplot2 包在颜色条上手动添加标签名称

我想通过为比例尺底部和顶部的值添加手动标签名称(低和高)来更改连续颜色图例。我怎样才能做到这一点?

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)

r ggplot2

2
推荐指数
1
解决办法
709
查看次数

为什么带有 %in% 的条件会忽略缺失值?

当我%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包含三十个可能的级别)。

r conditional-statements recode

2
推荐指数
1
解决办法
80
查看次数

标签 统计

r ×4

conditional-statements ×1

dplyr ×1

footnotes ×1

ggplot2 ×1

gt ×1

gtsummary ×1

recode ×1