r - 如何在 dplyr::across 的 .names 参数中使用字符串操作函数

标签 r dplyr across r-glue

虽然我试图搜索它是否重复,但我找不到类似的问题。 (虽然有一个similar,但这和我的要求有些不同)

我的问题是,我们是否可以在 .names 参数中使用 substrstringr::str_remove 等字符串操作函数dplyr::跨越。作为一个可重现的例子考虑这个

library(dplyr)
iris %>%
  summarise(across(starts_with('Sepal'), mean, .names = '{.col}_mean'))

  Sepal.Length_mean Sepal.Width_mean
1          5.843333         3.057333

现在我的问题是我想重命名输出列 str_remove(.col, 'Sepal') 这样我的输出列名称就是 Length.mean宽度均值 。为什么我要问是因为 description这个论点指出

.names
A glue specification that describes how to name the output columns. This can use {.col} to stand for the selected column name, and {.fn} to stand for the name of the function being applied. The default (NULL) is equivalent to "{.col}" for the single function case and "{.col}_{.fn}" for the case where a list is used for .fns.

我已经尝试了很多可能性,包括以下,但这些都不起作用

library(tidyverse)
library(glue)
iris %>%
  summarise(across(starts_with('Sepal'), mean, 
                   .names = glue('{xx}_mean', xx = str_remove(.col, 'Sepal'))))

Error: Problem with `summarise()` input `..1`.
x argument `str` should be a character vector (or an object coercible to)
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.
Run `rlang::last_error()` to see where the error occurred.


#OR
iris %>%
  summarise(across(starts_with('Sepal'), mean, 
                   .names = glue('{xx}_mean', xx = str_remove(glue('{.col}'), 'Sepal'))))

我知道这可以通过使用 rename_with 添加另一个步骤来解决,所以我不关心那个答案。

最佳答案

这可行,但可能有一些注意事项。您可以在粘合规范中使用函数,这样您就可以用这种方式清理字符串。但是,当我尝试转义 "." 时,出现错误,我认为这与 across 解析字符串的方式有关。如果您需要更动态的东西,您可能想在此时深入研究源代码。

为了使用 {.fn} 助手,至少要像这样动态创建粘合字符串,函数需要一个名称;否则你会在 .fns 参数中得到一个函数索引号。我用第二个函数对此进行了测试,并使用 lst 进行自动命名。

library(dplyr)
iris %>%
  summarise(across(starts_with('Sepal'), .fns = lst(mean, max), 
                   .names = '{stringr::str_remove(.col, "^[A-Za-z]+.")}_{.fn}'))
#>   Length_mean Length_max Width_mean Width_max
#> 1    5.843333        7.9   3.057333       4.4

关于r - 如何在 dplyr::across 的 .names 参数中使用字符串操作函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67548197/

相关文章:

r - 在跨函数的 .names 参数中使用函数

r - 在 R 上计算基于五分位数的分数

r - 修改和拆分 R 数据框中的行名称

r - 修改列表列中的数据框

r - 如何在 R 中按组计算 SD,而不丢失在 ggplot2 中绘图仍需要的列?

r - 如何动态创建具有整洁评估的列?

Rmarkdown投影仪: How to left justify the title page

从相关系数计算中删除异常值

r - dplyr:group_by 和 group_by_ 函数有什么区别?

r - Dplyr cross + mutate + condition 选择列