Questions tagged [across]

across is a function for use within the Tidyverse set of R packages which is intended to simplify the process of applying functions across columns of a data.frame. Use this tag to ask questions which focus on applying one or more functions to multiple columns with the across function or the rowwise version c_across.

240 questions
37
votes
2 answers

Renaming multiple columns with dplyr rename(across(

Hey i'm trying to rename some columsn by adding "Last_" with the new version of dplyr but I keep getting this error Error: `across()` must only be used inside dplyr verbs. this is my code data %>% rename(across(everything(), ~paste0("Last_",…
Ian.T
  • 1,016
  • 1
  • 9
  • 19
23
votes
4 answers

Concatenate column names in one column conditional on using mutate, across and case_when

I would like to: Use across and case_when to check if columns A1-A3 == 1 Concatenate the column names of the columns where A1-A3 == 1 and mutate a new column with the concatenated column names My dataframe: df <- tribble( ~ID, ~A1, ~A2, …
TarJae
  • 72,363
  • 6
  • 19
  • 66
8
votes
3 answers

Mutate, across, and case_when

I am having some trouble getting mutate, across, and case_when to function properly, I've recreated a simple version of my problem here: a <- c(1:10) b <- c(2:11) c <- c(3:12) test <- tibble(a, b, c) # A tibble: 10 x 3 a b c …
RobBot
  • 93
  • 1
  • 3
8
votes
4 answers

Can you use dplyr across() to iterate across pairs of columns?

I have 18 pairs of variable and I would like to do pair-wise math on them to calculate 18 new variables. The across() function in dplyr is quite handy when applying a formula to one column. Is there a way to apply across() to pairs of columns? Tiny…
nefosl
  • 366
  • 1
  • 8
8
votes
1 answer

across function not found in dplyr package

I want to use the across() function in dplyr but get an error. For instance, running iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), mean)) gives me Error in across(starts_with("Sepal"), mean) : could not find function…
Andrew
  • 678
  • 2
  • 9
  • 19
7
votes
4 answers

Combine: rowwise(), mutate(), across(), for multiple functions

This is somehow related to this question: In principle I try to understand how rowwise operations with mutate across multiple columns applying more then 1 functions like (mean(), sum(), min() etc..) work. I have learned that across does this job and…
TarJae
  • 72,363
  • 6
  • 19
  • 66
7
votes
1 answer

How to count rows by group with n() inside dplyr::across()?

In previous versions of dplyr, if I wanted to get row counts in addition to other summary values using summarise(), I could do something like library(tidyverse) df <- tibble( group = c("A", "A", "B", "B", "C"), value = c(1, 2, 3, 4,…
niclow
  • 73
  • 1
  • 4
7
votes
2 answers

dplyr::mutate all numeric variables except one?

Is there a way to mutate all numeric variables except one (in this case age) or two? data data = data.frame( Year = c(1,2,5,7,2,6,2,6), days = c(5,3,6,3,7,2,5,7), age = c(1,3,5,23,2,4,5,2), names = c("A063", "A013", "A063", "A083",…
Ian.T
  • 1,016
  • 1
  • 9
  • 19
6
votes
2 answers

Why `scale` when using mutate + across in dplyr create columns with `[,1]` at the end?

See code below. the mutate(across(everything(), scale, .names = "{.col}_z")) part of the syntax is generating columns with [,1]appended at the end. Two questions: Why is this happening? How can I avoid or remove it? library(dplyr) # Input df_test…
Ruam Pimentel
  • 1,288
  • 4
  • 16
6
votes
1 answer

How can I keep old columns and rename new columns when using `mutate` with `across`

When I mutate across data, the columns selected by .cols are replaced by the results of the mutation. How can I perform this operation whilst: Keeping the columns selected by .cols in the output Appropriately & automatically renaming the columns…
Captain Hat
  • 2,444
  • 1
  • 14
  • 31
6
votes
4 answers

How to mutate multiple columns as function of multiple columns systematically?

I have a tibble with a number of variables collected over time. A very simplified version of the tibble looks like this. df = tribble( ~id, ~varA.t1, ~varA.t2, ~varB.t1, ~varB.t2, 'row_1', 5, 10, 2, 4, 'row_2', 20, 50, 4, 6 ) I want to…
Maher Said
  • 170
  • 2
  • 15
6
votes
3 answers

R dplyr: how to use ... with summarize(across()) when ... will refer to a variable name within the data?

I want to have a flexible function using summarize in which: the aggregation function is given by user the aggregation function might use further arguments which refer to variables within the data itself. A good example is the user providing…
Matifou
  • 7,968
  • 3
  • 47
  • 52
6
votes
3 answers

How to combine the across () function with mutate () and case_when () to mutate values in multiple columns according to a condition?

I have demographic data set, which includes the age of people in a household. This is collected via a survey and participants are allowed to refuse providing their age. The result is a data set with one household per row (each with a household ID…
5
votes
2 answers

Dplyr across + mutate + condition to select the columns

I am sure the solution is a one liner, but I am banging my head against the wall. See the very short reprex at the end of the post; how do I tell dplyr that I want to double only the columns without NA? Many thanks library(dplyr) #> #> Attaching…
larry77
  • 1,309
  • 14
  • 29
5
votes
1 answer

How to refer to other column names within dplyr mutate across?

I want to use dplyr mutate across, and refer to another static column to be used for all mutate functions. df <- data.frame(baseline = c(1,2,3), day1 = c(NA,2,2), day2 = c(2,3,4), day3= c(5,4,6)) I want to make a new column 'fc' for the change from…
user42485
  • 751
  • 2
  • 9
  • 19
1
2 3
15 16