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
3
votes
3 answers

Replacing multiple columns from different dataframe using dplyr

I have two dataframes, one of which contains a subset of IDs and columns of the other (but has different values). ds1 <- data.frame(id = c(1:4), d1 = "A", d2 = "B", d3 = "C") ds2 <-…
FowlPlay
  • 95
  • 3
3
votes
1 answer

Can we actually pass two sets of multiple variables into mutate across in dplyr

This question though having three answers raised me doubts as I am mulling my head over the problem. Though I am aware that problem can be solved by other methods (and using purrr or apply group of functions especially), Yet I am not sure that can…
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
3
votes
1 answer

How do I use dplyr::across with a multi-argument function on a grouped data frame?

I want to compute a weighted moving average across multiple columns, using the same weights for each column. The weighted moving average shall be computed per group (in contrast to using `dplyr::across` with functions with more than one…
robust
  • 594
  • 5
  • 17
3
votes
2 answers

How to mutate several columns by column index rather than column name using across?

Building on this questions: R dplyr mutate on column index dplyr: how to reference columns by column index rather than column name using mutate? I want to mutate several columns using column indexes for both the source and the destination of the…
Guido Berning
  • 187
  • 2
  • 12
3
votes
1 answer

Using summarise, across, and quantile functions together

I am trying to use mtcars dataset to calculate summary statistics. Here is my code - df <- as_tibble(mtcars) df.sum2 <- df %>% select(mpg, cyl, vs, am, gear, carb) %>% mutate(across(where(is.factor), as.numeric)) %>% summarise(across( …
Sharif
  • 163
  • 1
  • 9
3
votes
3 answers

Conditionally replace values across multiple columns based on string match in a separate column

I'm trying to conditionally replace values in multiple columns based on a string match in a different column but I'd like to be able to do so in a single line of code using the across() function but I keep getting errors that don't quite make sense…
Natalie O'Shea
  • 197
  • 1
  • 8
2
votes
4 answers

Creating multiple NEW columns using across() in R

The difference between my question and existing questions is that I want to create new columns with mutate that do not depend on existing columns. Some dummy data: library(dplyr) dat <- tibble( a = 1:5, b = LETTERS[1:5] ) I know I can…
Earlien
  • 145
  • 9
2
votes
2 answers

How to impute a conditional row-wise imputation of a constant

I am somewhat of an R newbie, am struggling with writing code for what seems like simple logic, and would appreciate any help! I am trying to impute a constant value of 1 for NA cells in each row of my data set but only for rows that have 2 or less…
kaho
  • 23
  • 3
2
votes
2 answers

R - dplyr across: subtracting 1 column from a range of columns based on index

This is my first post, and I am relatively new to R, so apologies if I have framed this poorly. I have not found this problem described anywhere else but the initial approach is somewhat similar to that decribed here: How to mutate several columns…
engpol
  • 35
  • 5
2
votes
2 answers

How can I mutate using across and a dynamically-generated list of functions

I have a data frame which encapsulates a number of statistics for an exam, tracked over different years and groups. I would like to construct a function which adds new columns giving the change in these statistics for each group from a dynamically…
Stephen Morgan
  • 327
  • 1
  • 9
2
votes
1 answer

How to write anonymous functions in R arrow across

I have opened a .parquet dataset through the open_dataset function of the arrow package. I want to use across to clean several numeric columns at a time. However, when I run this code: start_numeric_cols = "sum" sales <- sales %>% mutate( …
2
votes
1 answer

What's a shorthand for `across(everything(), ...)` in dplyr?

I'm wondering if there's an obvious shorthand for applying a bunch of functions across the whole dataset, that is shorthand for across(everything(), ...). Solution does not have to use dplyr. Note that ! Using `across()` without supplying `.cols`…
Rico
  • 1,998
  • 3
  • 24
  • 46
2
votes
1 answer

Perform multiple operations on multiple columns with dplyr

I am working in R and have a dataframe with various series. I need to perform on most of these columns the following two operations: max(0,x_t - max(x_{t-1}, x_{t-2}, x_{t-3}, x_{t-4})) max(0,-1 + x_t / max(x_{t-1}, x_{t-2}, x_{t-3}, x_{t-4})) I…
Fef894
  • 59
  • 3
2
votes
2 answers

Is it possible to count by using the count function within across()?

Hello R and tidyverse wizards, I try to count the rows of the starwars data set to know how many observations we get with the variables "height" and "mass" . I managed to get it with this code: library(tidyverse) starwars %>% select(height, mass)…
Indy
  • 21
  • 2
2
votes
1 answer

How to apply this function columnwise with across() in tidyverse?

I need to create new columns based on existing ones using a function in tidyverse. I planned to use across() because it allows dynamic renaming of new variables which is increasingly important in my case and spares a lot of time especially if you…
doctorate
  • 1,381
  • 1
  • 19
  • 43