rdplyracross

In `dplyr/across` , how to exclude given columns


In dplyr/across, i want to *-1 to numeric columns exclude a1 or excelue a1 a2

library(tidyverse)    
raw_data <- data.frame(
  type=c('A','B','C'),
  cat=c("cat1","cat2","cat3"),
  a3=1:3,
  b2=1:3,
  c2=1:3,
  c1=1:3,
  a2=1:3,
  b1=1:3,
  a1=1:3)

#  * -1 to numeric columns exclude a1  
raw_data %>% mutate(across(where(is.numeric)&!='a1',~. *-1))

#  * -1 to numeric columns exclude a1 a2
raw_data %>% mutate(across(where(is.numeric)&!(. %in% c('a1','a2')),~. *-1))

Solution

  • The first argument in across is .cols, so just supply a vector of columns (c()) you want to in- or ex-clude.

    library(dplyr)
    
    raw_data |> 
      mutate(across(c(where(is.numeric), -a1, -a2), ~ . * -1))
    
      type  cat a3 b2 c2 c1 a2 b1 a1
    1    A cat1 -1 -1 -1 -1  1 -1  1
    2    B cat2 -2 -2 -2 -2  2 -2  2
    3    C cat3 -3 -3 -3 -3  3 -3  3