I've found that across()
is very useful for repeating operations on several columns.
However, I still haven't fully understood how to select specific columns for the operation.
Let's say that I want to apply a function to all columns in mtcars
, except gear
and carb
.
I tried something like
# Function to use over columns
demean <- function(x) {
x - mean(x, na.rm = TRUE)
}
# Use function on all but columns gear and carb
mtcars %>% mutate(across(.cols = select(.,-gear,-carb), demean))
However, this throws the error
Error: Problem with `mutate()` input `..1`.
x Must subset columns with a valid subscript vector.
x Subscript has the wrong type `data.frame<
What is the proper way to unselect certain columns in across
?