根据列类有条件地更改列 [英] Conditionally mutate columns based on column class

查看:78
本文介绍了根据列类有条件地更改列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题基于此处发布的上一个主题:互斥多个数据框中的列

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       ...           ...         ...         ...           ...

其中id是键,而char_var_x是字符变量,而num_var_x是数字变量.我总共有346列,我想编写一个可缩放除id列以外的所有数字变量的函数.我正在寻找一种使用管道和dplyr函数变异的优雅方法.

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

但是我正在寻找一种方法,以便从缩放中排除id列,并保留原始值,同时缩放所有其他数字变量.有没有一种优雅的方法可以做到这一点?

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?

推荐答案

尝试以下方法,答案类似于 select_if 帖子:

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

这篇关于根据列类有条件地更改列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆