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.
Questions tagged [across]
240 questions
0
votes
1 answer
Correct syntax for mutate across when excluding columns
I'm trying to convert code from using mutate_at to using mutate(across()). I'm assuming I have a syntax error but after 45min of trying to figure it out I decided it was time to engage this forum.
Reproducible…

DaveM
- 664
- 6
- 19
0
votes
1 answer
How can I convert this old dplyr syntax?
I am new to dplyr and I'm having difficulties in (i) understanding its syntax and (ii) transforming its old version code into a code I can use in its newest version (dplyr 1.0.2). In particular, I'm confused about the two following lines of code…

Ema
- 23
- 3
0
votes
2 answers
tidyverse use across with pasted external vector that contains column names to mutate
Let's assume the following data:
df <- data.frame(x = c(1, 2),
y = c(3, 4),
z = c(5, 6))
Let's further assume I have a vector that contains column names I want to work on, e.g.
var_names_1 <- c("test", "x",…

deschen
- 10,012
- 3
- 27
- 50
0
votes
1 answer
pandas: calculatig average similarity across all categories
I have a dataframe like the following but larger:
import pandas as pd
data = {'First': ['First value','Third value','Second value','First value','Third value','Second value'],
'Second': ['the old man is here','the young girl is there',…

zara kolagar
- 881
- 3
- 15
0
votes
5 answers
R - How to pass parameters to function in "mutate across"?
I have a dataframe which contains multiple columns with date-values as strings (format: YYYYMMDD).
dt_example <- data.frame (
W03 = "20201130",
W44 = "19711031",
P01 = "19740813",
P04 = "20000506",
Z02 = "20201231"
)
Now I…

benne
- 37
- 4
0
votes
1 answer
Across with NA values
I was trying to take the mean of some columns using across and there was an issue
making new different columns on for each column of the mean I used.
Is it working properly?
library(tidyverse)
cars %>%
as_tibble() %>%
add_case(speed = 11,…

K Y
- 198
- 2
- 16
0
votes
1 answer
Summarise(across(where)) in R
I have the following data file called "data_2":
Person Weight Height
A 55 155
B 65 165
C 75 175
I wanna use the Summarise(across(where))-command in order to generate the total weight and the weight for each person. This…

John Doe
- 37
- 8
0
votes
1 answer
How to mutate new columns across all combinations of other columns?
My point of departure is the whigs data from the ggraph package. It contains an incidence matrix.
Now, for each combination of columns/variables, I'd like to know if all the columns are 1 or not, and create a new column for that combination with a 1…

Alexander
- 72
- 7
0
votes
0 answers
Mutate with across and .names, how can I list the columns
I am trying to name the output of an across function using {fn} and a number for the column.
I have this idea for the command, but don't work:
iris %>%
mutate(
across(Sepal.Length:Petal.Width, list(var = ~ . -1),
.names =…
0
votes
2 answers
Access column names in a function to be used in dplyr::mutate
Using OECD data, I can retrieve a database where variables are specified by their IDs and a list with the corresponding labels. Here is a minimal example that reproduces the data structure:
df <-…

Massimo2013
- 533
- 4
- 17
0
votes
1 answer
Why is across() not working like it should?
I'm trying to understand how to use the across() function in order to replace _if, replace_at, variations. But I can't see why this code is not working. I've already checked the "colwise" vignette but it didn't help much.
hmeq <-…

igna43
- 11
- 3
0
votes
1 answer
Mutate specific columns based on position
I trying to convert all negative values to 0 in specific columns of a dataframe. How would I for an example convert negative values to zero in column 3:5 and 8 in the mtcars data?
mtcars <- mtcars%>%
mutate(across(c(3:5,8), funs(replace(., .<0,…

Tiptop
- 533
- 5
- 19
0
votes
1 answer
dplyr::across() does not find everything() or dot from pipe
Following the documentation of ?across:
across(.cols = everything(), .fns = NULL, ..., .names = NULL)
I used everything() as reference to the LHS.
Yet neither everything() nor . seems to receive the LHS object from the pipe.
What am I…

Agile Bean
- 6,437
- 1
- 45
- 53
0
votes
1 answer
summarize across -- is it order dependent?
I came across something weird with dplyr and across, or at least something I do not understand.
If we use the across function to compute the mean and standard error of the mean across multiple columns, I am tempted to use the following…

vashts85
- 1,069
- 3
- 14
- 28
0
votes
1 answer
How may I rowSum over a subset of variables by name with expression like a:b
dd <- data.frame(a=1:10,b=2:11,c=3:12)
dd %>% mutate( Total=rowSums(.[1:2]))
a b c CCI
1 1 2 3 3
2 2 3 4 5
3 3 4 5 7
4 4 5 6 9
5 5 6 7 11
6 6 7 8 13
7 7 8 9 15
8 8 9 10 17
9 9 10 11 19
10 10 11 12 …

Z. Zhang
- 637
- 4
- 16