r - 使用基础 R 从 dplyr::mutate_at() 复制结果
问题描述
我正在尝试复制dplyr::mutate_at()
使用 base R 的结果。我自己编写函数相当新,我想知道我想出的函数是否(a)合理,(b)我如何cbind()
在函数内部进行调用和还保留diamonds
数据集中的所有变量。
首先dplyr::mutate_at()
调用:
require(tidyverse)
diamonds %>%
mutate_at(.funs = funs(relative = ./price), .vars = c("x", "y", "z"))
# A tibble: 53,940 x 13
#carat cut color clarity depth table price x y z x_relative y_relative z_relative
#<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 0.0121 0.0122 0.00745
#2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 0.0119 0.0118 0.00709
#3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 0.0124 0.0124 0.00706
#4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63 0.0126 0.0127 0.00787
#5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 0.0130 0.0130 0.00821
#6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 0.0117 0.0118 0.00738
#7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47 0.0118 0.0118 0.00735
#8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53 0.0121 0.0122 0.00751
#9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49 0.0115 0.0112 0.00739
#10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39 0.0118 0.0120 0.00707
# ... with 53,930 more rows
这是我想出的在基础 R 中复制结果的函数:
rel_fun <- function(x, y){
out <- x / y
colnames(out) <- (paste(colnames(x), "relative", sep = "_"))
out
}
结果如下:
df_out <- rel_fun(diamonds[c("x", "y", "z")], diamonds$price)
df_out2 <- cbind(diamonds, df_out)
head(df_out2)
#carat cut color clarity depth table price x y z x_relative y_relative z_relative
#1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 0.01211656 0.01220859 0.007453988
#2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 0.01193252 0.01177914 0.007085890
#3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 0.01238532 0.01244648 0.007064220
#4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63 0.01257485 0.01266467 0.007874251
#5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 0.01295522 0.01298507 0.008208955
#6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 0.01172619 0.01178571 0.007380952
我想说,这一切都很好,但正如我上面提到的,如何在函数中保留diamonds
数据集的所有变量cbind()
?
我尝试了以下但我不会得到diamonds
数据集的其他变量,因为我没有在函数中添加它们。我只添加了计算所需的那些,即diamonds[c("x", "y", "z")]
. 有没有办法在函数中添加一些东西,让我保留原始数据集的其他变量?
rel_fun <- function(x, y){
out <- x / y
colnames(out) <- (paste(colnames(x), "relative", sep = "_"))
out2 <- cbind(x, out)
out2
}
df_out3 <- rel_fun(diamonds[c("x", "y", "z")], diamonds$price)
head(df_out3)
# x y z x_relative y_relative z_relative
#1 3.95 3.98 2.43 0.01211656 0.01220859 0.007453988
#2 3.89 3.84 2.31 0.01193252 0.01177914 0.007085890
#3 4.05 4.07 2.31 0.01238532 0.01244648 0.007064220
#4 4.20 4.23 2.63 0.01257485 0.01266467 0.007874251
#5 4.34 4.35 2.75 0.01295522 0.01298507 0.008208955
#6 3.94 3.96 2.48 0.01172619 0.01178571 0.007380952
解决方案
管道运算符%>%
将您的数据帧diamonds
作为第一个参数隐式传递给mutate_at()
. 为了模仿它的行为,你需要对你的函数做同样的事情。因为您会将整个数据框传递给函数,所以您也可以将列名传递为x
:
rel_fun <- function(.data, x, y){
out <- .data[x] / y
colnames(out) <- (paste(x, "relative", sep = "_"))
out2 <- cbind(.data, out)
out2
}
rel_fun( diamonds, c("x", "y", "z"), diamonds$price ) # Works as desired
推荐阅读
- python-3.x - Pandas groupby 值并将观察计数返回到数据集
- macos - 命令 dotnet run 在 Mac 上静默失败
- node.js - 如何将只有 1 个额外嵌套状态的状态应用于 MongoDB
- machine-learning - 特征选择还是 PCA?
- javascript - input 和 keyPress 事件的区别
- python - python - 按嵌套中的项目总数对嵌套字典进行排序?
- c++ - 模板类上的重载运算符
- c - 错误 LNK2001:未解析的外部符号时钟()
- angularjs - 当我在 AngularJS 中动态创建 opengraph 标签时,Facebook 共享页面没有缩略图
- oracle - 遇到 ORA-29106:无法导入 PKCS#12 钱包错误