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
5
votes
2 answers

mutate with across and .names: "glue cannot interpolate functions into strings" error

I have election results for different candidates by district. The source has the number of votes for each candidate and the total number of votes per district. I'd like to add variables for the percentage of votes that each candidate got in each…
vgXhc
  • 89
  • 6
5
votes
1 answer

How can I use dplyr across() programmatically on no variables?

Issue: I want to use across() programmatically so that if, e.g. NULL or an empty string is passed to it, the function won't fail. This is possibly using scoped variants of functions such as group_by_at(), but I'd like to make it work neatly (i.e.…
wurli
  • 2,314
  • 10
  • 17
5
votes
3 answers

Refer to column names dynamically inside mutate statements - dplyr

I stat apologize for the long question, but after quite a while I couldn't figure out a solution myself. I have this toy dataframe set.seed(23) df <- tibble::tibble( id = paste0("00", 1:6), cond = c(1, 1, 2, 2, 3, 3), A_1 = sample(0:9, 6,…
Ric S
  • 9,073
  • 3
  • 25
  • 51
4
votes
1 answer

Specify a column type across multiple columns with tidy-selection in readr package

I attempt to use read_csv from {readr} to read a CSV file into R. To demonstrate my real issue, I reset the argument guess_max to 5 at first (default is 1000) library(readr) formals(read_csv)$guess_max <- 5 and take a smaller literal data for…
user18894435
  • 373
  • 1
  • 10
4
votes
4 answers

How to convert columns to multiple boolean columns with tidyverse

I have a group of columns for each time and I want to convert it to a lot of boolean columns (one by category) with mutate() and across() like that : data <- data.frame(category_t1 = c("A","B","C","C","A","B"), category_t2 =…
jrdavalos
  • 75
  • 4
4
votes
1 answer

How to transform this base R code to dplyr using across and .names arguement

I have this dataframe: df <- structure(list(A = c(2L, 3L, 4L, 5L, 5L), B = c(3L, 1L, 2L, 5L, 5L), C = c(4L, 5L, 2L, 1L, 1L), D = c(3L, 1L, 5L, 1L, 2L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L)) A B C D …
TarJae
  • 72,363
  • 6
  • 19
  • 66
4
votes
2 answers

Dplyr: add multiple columns with mutate/across from character vector

I want to add several columns (filled with NA) to a data.frame using dplyr. I've defined the names of the columns in a character vector. Usually, with only one new column, you can use the following pattern: test %>% mutate(!!new_column :=…
starja
  • 9,887
  • 1
  • 13
  • 28
4
votes
2 answers

Why does my R mutate across with rowSums not work (Error: Problem with `mutate()` input `..2`. x 'x' must be numeric ℹ Input `..2` is `rowSums(.)`.)?

I'm trying to learn how to use the across() function in R, and I want to do a simple rowSums() with it. However, I keep getting this error: Error: Problem with mutate() input ..2. x 'x' must be numeric ℹ Input ..2 is rowSums(., na.rm = TRUE). Yet,…
J.Sabree
  • 2,280
  • 19
  • 48
4
votes
2 answers

How to subtract two columns using tidyverse mutate with columns named by external variables

I’d like to dynamically assign which columns to subtract from each other. I’ve read around and looks like I need to use all_of, and maybe across (How to subtract one column from multiple columns in a dataframe in R using dplyr, How to you use…
Mike
  • 921
  • 7
  • 26
4
votes
2 answers

How to use across and mutate across an entire dataset that has multiple column types?

I'm trying to use dplyr's across and case_when across my entire dataset, so whenever it sees "Strongly Agree" it changes it to a numeric 5, "Agree" to a numeric 4, and so on. I've tried looking at this answer, but I'm getting an error because my…
J.Sabree
  • 2,280
  • 19
  • 48
4
votes
4 answers

Using dplyr::across with two sets of variables

I have two sets of variables, for example variable a and variable a_avail. I am trying to change the value of a based on the value of a_avail and am wondering if this can be done using across with glue. Here is what I have tried. No error is…
Jessica
  • 75
  • 5
4
votes
4 answers

Mutate across with ifelse and is.na

I have data in the format outlined below, where all of the variables I need to work with are either NA or the name of the variable, and I need to change the NAs to 0 and the strings to 1. I'm trying to use dplyr::across() and ifelse(), but what gets…
Emma
  • 63
  • 1
  • 6
4
votes
1 answer

Combining multiple across() in a single mutate() sentence, while controlling the variables names in R

I have the following dataframe: df = data.frame(a = 10, b = 20, a_sd = 2, b_sd = 3) a b a_sd b_sd 1 10 20 2 3 I want to compute a/a_sd, b/b_sd, and to add the results to the dataframe, and name them ratio_a, ratio_b. In my dataframe I…
Rtist
  • 3,825
  • 2
  • 31
  • 40
4
votes
1 answer

Use column names in functions from mutate(across()) in R

I am trying to compare col with the col_new in my data and create a new boolean identifier called change_col which identifies the changes. Below is an example: input <- data.frame( apple = c(1,2) ,apple_new = c(2,3) ,banana = c(1,2) …
Daves
  • 175
  • 1
  • 10
4
votes
1 answer

Summarise across list columns, get first value, in dplyr

I have a large dataframe with some semi-duplicate entries I'm trying to merge via dplyr::summarise. This works fine for numeric, character, & logical columns, but not list columns. mydata <- tibble(A = c(1,1,2,2,3,3), B =…
dez93_2000
  • 1,730
  • 2
  • 23
  • 34
1
2
3
15 16