r - 使用 mutate_at 对多个变量进行 Boxcox 变换
问题描述
说,我想对以下数据(不是我正在使用的数据,只是为了解释我的问题)从 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(.)))
推荐阅读
- php - 使用 php api 登录多个 Instagram 帐户
- java - Android 相机 API2 不支持的文件格式
- javascript - 如何将base64解码为vue.js中输入类型文件中的文件格式?
- azure - 无法在本地运行 azure 函数调试 - 无法加载类型 Microsoft.Azure.WebJobs.Host.Scale.IScaleMonitorProvider
- android - 数据未在 MVVM 模型中的数据绑定示例中更新 - 将数据更新到模型和从模型更新
- parsing - 如何使用 megaparsec 报告多个错误?
- sqlite - 如何使用 lua 固件在 ESP8266 上运行 sqlite3?
- python - 使用 MDM 从 iOS 设备安装或删除应用程序时,我可以收到通知或任何观察者吗?
- celery - 如何知道 Celery 的所有任务都完成了?
- java - 如何使用 Android 在 Firebase 实时数据库中检索经过身份验证的用户的用户数据