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
How can I mutate across all columns of a dataframe using a function like separate that seems to be expecting a dataframe instead of a vector?
I have a dataframe where I would like to separate each column into two columns. Each column follow the same pattern "x_y"
test <- structure(list(A = c("511686_0.112", "503316_0.105", "476729_0.148",
"229348_0.181", "385774_0.178", "209277_0.029",…

canderson156
- 1,045
- 10
- 24
0
votes
1 answer
How to mutate across multiple columns and paste the relevant column name as the cell entry?
I'm trying to mutate across multiple columns to paste in the column name as the entry where the entries are/are not NA.
Using airquality dataset
> head(airquality)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 …

lilblue
- 84
- 7
0
votes
2 answers
mutate across columns with ifelse
I'm trying to mutate values across multiple columns to values from another column.
This is my dataset:
library(stringr)
library(dplyr)
library(fastDummies)
score <- sample(1:100,20,replace=TRUE)
df <- data.frame(score)
df <- df %>%
mutate(grp =…

Bruh
- 277
- 1
- 6
0
votes
0 answers
How to mutate() columns to as.double() across() multiple columns except for certain?
Trying to do a mutate across multiple columns for a data frame:
id
name
col_1
col_2
col_3
..
col_x
xyz
abc
3
4
3
..
3
This is the code that I currently have, and it works just fine:
data_frame$col1 %<>% as.double()
data_frame$col2…

spaceriker
- 1
- 2
0
votes
0 answers
Error in dplyr::summarise(): Caused by error in across()
I have this code below:
library(dplyr)
dfDeplaned <- dfDeplaned %>%
dplyr::group_by(Activity.Period, GEO.Region, Adjusted.Activity.Type.Code) %>%
dplyr::summarise(across(c(Adjusted.Passenger.Count), sum))
What I want to do is to sum up all the…

Anonymous User
- 15
- 4
0
votes
0 answers
Create new columns from multiple conditions across multiple columns
I need to create 2 new columns based on the conditions across 7 columns for over a million rows. Sample data is below.
I need newcol1 to return 1/0 if R ==1 and if any or all of the 6 columns D:N ==1. R==0 regardless of D:N value
I need newcol2 to…

Lilly
- 1
- 1
0
votes
1 answer
How to get the number of "yes" values across a group of variables
I have a dataset in Stata with information on whether or not a participant has experienced a specific side effect. The dataset contains about 900 participants and there are 20 side effects captured for each participant. The values for each side…

Becky
- 1
- 1
0
votes
1 answer
Timing of evaluation of several across inside same summarize in dplyr pipe
I'm wondering the way R is evaluating several across in the same summarise inside a dplyr piping. Consider the following example:
data(iris)
iris_summary <- iris %>%
group_by(Species) %>%
summarise(
across(
.cols =…

emaoca
- 47
- 3
0
votes
3 answers
Get column value associated to another column maximum in dplyr's across
After grouping by species and taken max Sepal.Length (column 1) for each group I need to grab the value of column 2 to 4 that are associated to maximum value of column 1 (by group). I'm able to do so for each single column at once but not in an…

emaoca
- 47
- 3
0
votes
0 answers
Mutate, across, contains to sum
I want to create a new column that sums up values from existing columns.
Consider iris dataset:
iris1 <- iris %>% mutate(totalSepalLength = across(contains("Sepal"), sum))
This gives me a new column which is of type tibble. If I access…

Pss
- 553
- 4
- 12
0
votes
1 answer
collapse rows, keep all unique variable values in one column and all values in other column
I need to take this data and group all matching values in column ESVId, while retaining each unique value in column match; and all values in column Form that were associated with each value in columnmatch(there may be…

sscoresby
- 67
- 5
0
votes
2 answers
How can I recode several values based on another data frame in several variables in R?
I have a data set with many columns (DATA_OLD) in which I want to exchange all values based on an allocation list with many entries (KEY).
Every value in DATA_OLD should be replaced by its counterpart (can be seen in KEY) to create DATA_NEW.
For…

Julian ter Horst
- 5
- 2
0
votes
2 answers
mutate across columns - if condition met return column name as value
I have a wide data frame similar to document term matrix:
df_names_tkns <- tibble::tribble(
~name, ~aaa, ~ddd, ~downing, ~eee, ~london, ~street, ~bbb, ~broadway, ~ccc, ~new, ~york,
"AAA LONDON DOWNING STREET DDD…

Jacek Kotowski
- 620
- 16
- 49
0
votes
0 answers
How to use R's dplyr to group by date and then get the sum in several specific columns for each group?
I have a large dataset from some content analysis, with documents from different dates and a large number of columns with "1" if the document contains a statement of particular Code type and "0" if it does not.
I would like to group these rows by…

Nick Olczak
- 305
- 3
- 14
0
votes
2 answers
Get the rowwise minimum of certain columns excluding 0 and NA
I have made a very complex solution to something I feel should have a much simpler solution.
In short what I want:
I want to compute a new column containing the minimum value across 3 columns
I want to ignore zeros and NAs
If I only have zeros and…

Nina van Bruggen
- 393
- 2
- 13