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

83 Views Asked by At

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))
2

There are 2 best solutions below

0
benson23 On BEST ANSWER

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
0
ThomasIsCoding On

You can try !matches like below

raw_data %>%
    mutate(across(!matches("a[12]") & where(is.numeric), ~ . * -1))

which gives

  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