Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally mutate columns based on column class

Tags:

r

pipe

dplyr

My question is based on a previous topic posted here: Mutating multiple columns in a data frame

Suppose I have a tibble as follows:

id   char_var_1   char_var_2   num_var_1   num_var_2  ... x_var_n
1       ...           ...         ...         ...           ...
2       ...           ...         ...         ...           ...
3       ...           ...         ...         ...           ...

where id is the key and char_var_x is a character variable and num_var_x is a numerical variable. I have 346 columns in total and I want to write a function that scales all the numerical variables except the id column. I'm looking for an elegant way to mutate these columns using pipes and dplyr functions.

Obviously the following works for all numeric variables:

pre_process_data <- function(dt)
{
  # scale numeric variables
  dt %>% mutate_if(is.numeric, scale)
}

But I'm looking for a way to exclude id column from scaling and retain the original values and at the same time scale all other numerical variables. Is there an elegant way to do this?

like image 994
tadzi Avatar asked Jan 03 '23 22:01

tadzi


1 Answers

Try below, answer is similar to select_if post:

library(dplyr)

# Using @Psidom's example data: https://stackoverflow.com/a/48408027

df %>%
  mutate_if(function(col) is.numeric(col) &
              !all(col == .$id), scale)
#   id a  b
# 1  1 a -1
# 2  2 b  0
# 3  3 c  1
like image 61
zx8754 Avatar answered Jan 05 '23 14:01

zx8754