ibm
ibm

Reputation: 864

mutate(across()) with two conditions

I want to mutate the chr columns into factors, but exclude the first 15 columns (some of which are chr). I can only seem to mutate everything with mutate(across(where(is.character))... or mutate(across(.cols = -c(1:15)).... Additionally, I want to save the changes into the dataset (%<>%) so I cannot just select out 1:15 first. For example, how would you starwars%<>% mutate_if(is.character, as.factor) but excluding name.

Upvotes: 1

Views: 1765

Answers (1)

akrun
akrun

Reputation: 887048

We may use

library(dplyr)
library(magrittr)
starwars %<>%
    mutate(across(c(where(is.character),-(1:5)), factor))

Upvotes: 6

Related Questions