根据列名向量缩放列
scale columns based on vector of column names
set.seed(123)
dat <-
data.frame(year_ref = 2000:2004,
www_val1 = sample(5),
www_val2 = sample(5),
www_val3 = sample(5),
sat_val1 = sample(5),
sat_val2 = sample(5),
sat_val3 = sample(5),
ds_val1 = sample(5),
ds_val2 = sample(5),
ds_val3 = sample(5))
我想缩放其名称在另一个向量中提供的所有列。例如。向量 var_names
有 ds
和 sat
,我想缩放名称以它们开头的所有列
var_names <- c("ds", "sat")
library(dplyr)
dat %>%
dplyr::select(contains(var_names)) %>%
dplyr::mutate(scale(., center = T, scale = T))
但是,这是在创建新列。我可以实施如下解决方案以便进行更改吗
仅在原始数据框中,除了我不想硬编码列索引
dat[, 5:10] <- apply(dat[, 5:10], 2, function(x) scale(x, center = T, scale = T))
在dplyr
version >= 1.0.0 中你可以使用函数across
将一个函数应用于所有满足特定条件的列
library(dplyr)
dat %>%
mutate(across(starts_with(var_names), scale))
# year_ref www_val1 www_val2 www_val3 sat_val1 sat_val2 sat_val3 ds_val1 ds_val2 ds_val3
# 1 2000 3 3 2 -1.2649111 0.0000000 -1.2649111 -1.2649111 -0.6324555 -0.6324555
# 2 2001 2 1 3 0.6324555 0.6324555 -0.6324555 0.0000000 -1.2649111 0.0000000
# 3 2002 5 2 1 1.2649111 -0.6324555 0.0000000 0.6324555 0.0000000 0.6324555
# 4 2003 4 5 4 0.0000000 -1.2649111 0.6324555 1.2649111 0.6324555 1.2649111
# 5 2004 1 4 5 -0.6324555 1.2649111 1.2649111 -0.6324555 1.2649111 -1.2649111
library(tidyverse)
set.seed(123)
dat <-
data.frame(year_ref = 2000:2004,
www_val1 = sample(5),
www_val2 = sample(5),
www_val3 = sample(5),
sat_val1 = sample(5),
sat_val2 = sample(5),
sat_val3 = sample(5),
ds_val1 = sample(5),
ds_val2 = sample(5),
ds_val3 = sample(5))
var_names <- c("ds", "sat")
dat %>%
dplyr::mutate_at(vars(starts_with(var_names)), ~scale(., center = T, scale = T))
# year_ref www_val1 www_val2 www_val3 sat_val1 sat_val2 sat_val3 ds_val1 ds_val2 ds_val3
# 1 2000 3 3 1 0.0000000 -0.6324555 -1.2649111 0.6324555 0.6324555 0.0000000
# 2 2001 5 5 3 -1.2649111 0.0000000 0.0000000 -0.6324555 -1.2649111 1.2649111
# 3 2002 2 2 2 0.6324555 0.6324555 0.6324555 0.0000000 1.2649111 -0.6324555
# 4 2003 4 4 5 -0.6324555 -1.2649111 -0.6324555 1.2649111 -0.6324555 -1.2649111
# 5 2004 1 1 4 1.2649111 1.2649111 1.2649111 -1.2649111 0.0000000 0.6324555
set.seed(123)
dat <-
data.frame(year_ref = 2000:2004,
www_val1 = sample(5),
www_val2 = sample(5),
www_val3 = sample(5),
sat_val1 = sample(5),
sat_val2 = sample(5),
sat_val3 = sample(5),
ds_val1 = sample(5),
ds_val2 = sample(5),
ds_val3 = sample(5))
我想缩放其名称在另一个向量中提供的所有列。例如。向量 var_names
有 ds
和 sat
,我想缩放名称以它们开头的所有列
var_names <- c("ds", "sat")
library(dplyr)
dat %>%
dplyr::select(contains(var_names)) %>%
dplyr::mutate(scale(., center = T, scale = T))
但是,这是在创建新列。我可以实施如下解决方案以便进行更改吗 仅在原始数据框中,除了我不想硬编码列索引
dat[, 5:10] <- apply(dat[, 5:10], 2, function(x) scale(x, center = T, scale = T))
在dplyr
version >= 1.0.0 中你可以使用函数across
将一个函数应用于所有满足特定条件的列
library(dplyr)
dat %>%
mutate(across(starts_with(var_names), scale))
# year_ref www_val1 www_val2 www_val3 sat_val1 sat_val2 sat_val3 ds_val1 ds_val2 ds_val3
# 1 2000 3 3 2 -1.2649111 0.0000000 -1.2649111 -1.2649111 -0.6324555 -0.6324555
# 2 2001 2 1 3 0.6324555 0.6324555 -0.6324555 0.0000000 -1.2649111 0.0000000
# 3 2002 5 2 1 1.2649111 -0.6324555 0.0000000 0.6324555 0.0000000 0.6324555
# 4 2003 4 5 4 0.0000000 -1.2649111 0.6324555 1.2649111 0.6324555 1.2649111
# 5 2004 1 4 5 -0.6324555 1.2649111 1.2649111 -0.6324555 1.2649111 -1.2649111
library(tidyverse)
set.seed(123)
dat <-
data.frame(year_ref = 2000:2004,
www_val1 = sample(5),
www_val2 = sample(5),
www_val3 = sample(5),
sat_val1 = sample(5),
sat_val2 = sample(5),
sat_val3 = sample(5),
ds_val1 = sample(5),
ds_val2 = sample(5),
ds_val3 = sample(5))
var_names <- c("ds", "sat")
dat %>%
dplyr::mutate_at(vars(starts_with(var_names)), ~scale(., center = T, scale = T))
# year_ref www_val1 www_val2 www_val3 sat_val1 sat_val2 sat_val3 ds_val1 ds_val2 ds_val3
# 1 2000 3 3 1 0.0000000 -0.6324555 -1.2649111 0.6324555 0.6324555 0.0000000
# 2 2001 5 5 3 -1.2649111 0.0000000 0.0000000 -0.6324555 -1.2649111 1.2649111
# 3 2002 2 2 2 0.6324555 0.6324555 0.6324555 0.0000000 1.2649111 -0.6324555
# 4 2003 4 4 5 -0.6324555 -1.2649111 -0.6324555 1.2649111 -0.6324555 -1.2649111
# 5 2004 1 1 4 1.2649111 1.2649111 1.2649111 -1.2649111 0.0000000 0.6324555