mutate_at 对多个变量进行 Boxcox 变换
Boxcox transformation on multiple variables with mutate_at
说,我想对以下数据(不是我正在使用的数据,只是为了解释我的问题)从 caret 包进行 boxcox 转换:
library(caret); library(tidyverse)
set.seed(001)
d <- tibble(a = rpois(20, 10), b = rnorm(20, 40, 10))
head(d)
# A tibble: 6 x 2
a b
<int> <dbl>
1 8 20.1
2 10 46.2
3 7 39.4
4 11 38.4
5 14 25.3
6 12 35.2
我可以通过 运行
实现
d1 <- BoxCoxTrans(d$a) %>% predict(d$a)
我可以重复相同的过程来转换 b。有没有办法可以使用 dplyr 同时对变量 a 和 b 进行 boxcox 转换?我尝试了以下但我无法弄清楚如何编写 .funs
d %>% mutate_at(c("a", "b"), BoxCoxTrans %>% predict(d))
我从未使用过插入符号,但这些解决方案是否有任何理由在您的特定情况下不起作用? (他们 运行 对我来说很好。)
library(tidyverse)
library(caret)
library(e1071)
set.seed(001)
d <- tibble(a = rpois(20, 10), b = rnorm(20, 40, 10))
head(d)
#On selected columns
d %>%
mutate_at(vars(a,b), funs( BoxCoxTrans(.) %>% predict(.)))
#Or on all columns
d %>%
mutate_all(funs( BoxCoxTrans(.) %>% predict(.)))
说,我想对以下数据(不是我正在使用的数据,只是为了解释我的问题)从 caret 包进行 boxcox 转换:
library(caret); library(tidyverse)
set.seed(001)
d <- tibble(a = rpois(20, 10), b = rnorm(20, 40, 10))
head(d)
# A tibble: 6 x 2
a b
<int> <dbl>
1 8 20.1
2 10 46.2
3 7 39.4
4 11 38.4
5 14 25.3
6 12 35.2
我可以通过 运行
实现d1 <- BoxCoxTrans(d$a) %>% predict(d$a)
我可以重复相同的过程来转换 b。有没有办法可以使用 dplyr 同时对变量 a 和 b 进行 boxcox 转换?我尝试了以下但我无法弄清楚如何编写 .funs
d %>% mutate_at(c("a", "b"), BoxCoxTrans %>% predict(d))
我从未使用过插入符号,但这些解决方案是否有任何理由在您的特定情况下不起作用? (他们 运行 对我来说很好。)
library(tidyverse)
library(caret)
library(e1071)
set.seed(001)
d <- tibble(a = rpois(20, 10), b = rnorm(20, 40, 10))
head(d)
#On selected columns
d %>%
mutate_at(vars(a,b), funs( BoxCoxTrans(.) %>% predict(.)))
#Or on all columns
d %>%
mutate_all(funs( BoxCoxTrans(.) %>% predict(.)))