Though I tried to search whether it is duplicate, but I cannot find similar question. (though a similar one is there, but that is somewhat different from my requirement)
My question is that whether we can use string manipulation function such substr
or stringr::str_remove
inside .names
argument of dplyr::across
. As a reproducible example consider this
library(dplyr)
iris %>%
summarise(across(starts_with('Sepal'), mean, .names = '{.col}_mean'))
Sepal.Length_mean Sepal.Width_mean
1 5.843333 3.057333
Now my problem is that I want to rename output columns say str_remove(.col, 'Sepal')
so that my output column names are just Length.mean
and Width.mean
. Why I am asking because, the description of this argument states that
.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.
I have tried many possibilities including the following, but none of these work
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'))))
I know that this can be solved by adding another step using rename_with
so I am not looking after that answer.