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
4
votes
0 answers

Why is across slower than summarize_all in dplyr 1.0.0?

I just updated to the latest dplyr version 1.0.0. Trying out the new features in the summarize function, like across and .groups. But to my surprise, my code has become very slow compared to the former version. Is this a known issue? Am I doing…
Gijs
  • 81
  • 1
  • 8
3
votes
3 answers

Dynamic if else for both source and replacement columns in dplyr across

I have this table: df <- data.frame(value_2022 = c(1, NA, 3), volume_2022 = c(NA, 2, 3), value_2022_replacement = c(1.5, 2.5, 3.5), volume_2022_replacement = c(0.5, 1.5, 2.5)) df #> value_2022…
3
votes
4 answers

Mutate across pairs of columns with dplyr

I am trying to apply a function to pairs of columns in dplyr. In the simple example below, there are columns a and b, as well as a_exp and b_exp. I want to create a new column called a_mul = a*a_exp and another one for b (and c, d, e ...). I know I…
David
  • 31
  • 2
3
votes
3 answers

How do I mutate across to multiple columns together that have similar names in R?

I have many columns that have same names that always start with the same string, either n_ for the number of students, score_ for the percent of students who passed, and loc_ for the room number. In this, I want to multiple the n_ columns with their…
J.Sabree
  • 2,280
  • 19
  • 48
3
votes
2 answers

In dplyr mutate across, is it possible to use non-referenced columns in a programmable fashion?

Suppose I have this tibble with an arbitrary number of variable pairs x and x_var, y and y_var, etc. dt <- tibble(x = 1:3, y = 2:4, z = 3:5, x_var = rep(0.1, 3), y_var = rep(0.2, 3), z_var = rep(0.3, 3)) I was…
lmyt
  • 71
  • 2
3
votes
4 answers

How subtract two columns correlativelly in a data frame

Context We would like to know how subtract columns, two by two. Specifically, we want to subtract the columns of the dataframe as follow: the u_2018 - u_2019. the u_2019 - u_2020. the u_2020 - u_2021. the u_2021 - u_2022. We looked for a optimal…
3
votes
1 answer

how to apply a function(x,y) with two variables across set of variables ending with .x and .y using dplyr

Sample data: sampdat <- data.frame(grp=rep(c("a","b","c"),c(2,3,5)), x1=seq(0,.9,0.1),x2=seq(.3,.75,0.05), y1=c(1:10), y2=c(11:20)) I would like to have the following data, but i have 100+ variables for which i'd like to apply a function with two…
Sam
  • 33
  • 3
3
votes
2 answers

How to modify a column named in another column using adjacent column in tidyverse?

I've the following dataframe, df <- tibble(x = c(2, 3, 4)) %>% mutate(`1` = 99, `2` = 88, `3` = 77, `4` = 66, `5` = 55) column x holds the column names which needs to be manipulated, the value in that column has to be replaced with the sum of…
msunij
  • 309
  • 2
  • 8
3
votes
1 answer

How to use mutate across with a custom function with multiple arguments

I have created this custom function with the help of @jared_mamrot Make a custom function of an dplyr procedure It basically takes a dataframe, a column and a number as argument and replaces in that column a defined percent (y) of values with…
TarJae
  • 72,363
  • 6
  • 19
  • 66
3
votes
3 answers

How to use a function that returns multiple values in dplyr::across()?

I want to perform several operations on multiple columns and I can use dplyr::across() to do it that way: library(tidyverse) df = tibble(x=1:5, p1=x*2, p2=x*4, p3=x*5) r1 = df %>% mutate(across(starts_with("p"), c(inf=~.x-1, sup=~.x+1))) r1 #>…
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
3
votes
2 answers

Meaning of tilde and dot notation in dplyr

Summary I was reading an article on the subject of dplyr's across function. Looking at the first example of use, I saw the use of operators that I have never seen before. I do not know if they are inherently apart of dplyr or from some other…
student-R
  • 41
  • 3
3
votes
4 answers

Function to coerce strings given a condition

I simply want to coerce as numbers --i.e., to apply as.numeric to--any columns which have a 1 as their first entry (i.e., a character). So I would expect to turn: tibble(a = c("1", "2"), b = c("Fred", "Kevin"), c = 1:2) into tibble(a = 1:2, b =…
James Bejon
  • 189
  • 5
3
votes
2 answers

New dplyr version 1.0.6 (or couldve been sooner) seems to misuse plyr . dot in dplyr mutate across

I had this following code that worked just fine before my R updated from version 4.0 to 4.1 note, this is all part of a helper function I've written for a piece of software I'm developing, and I've replaced things accordingly. Without context, this…
Voy
  • 99
  • 4
3
votes
4 answers

Summary statistics for multiple variables with statistics as rows and variables as columns?

I'm trying to use dplyr::summarize() and dplyr::across() to obtain a tibble with several summary statistics in the rows and the variables in the columns. I was only able to achieve this result by using dplyr::bind_rows(), but I'm wondering if…
Lucas De Abreu Maia
  • 598
  • 2
  • 6
  • 19
3
votes
1 answer

How to use string manipulation functions inside .names argument in dplyr::across

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…
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
1 2
3
15 16