[英]Using dplyr mutate_at with custom function
我想从表中取出两个变量并将它们除以第三个变量并将这些计算添加为两个新列。 mutate_at
让我非常接近,但在自定义函数f
下面,我想访问数据集中的另一列。 任何建议或替代的整洁工具方法?
library(dplyr)
# this works fine but is NOT what I want
f <- function(fld){
fld/5
}
# This IS what I want where wt is a field in the data
f <- function(fld){
fld/wt
}
mutate_at(mtcars, .vars = vars(mpg, cyl), .funs = funs(xyz = f))
# This works but is pretty clumsy
f <- function(fld, dat) fld/dat$wt
mutate_at(mtcars, .vars = vars(mpg, cyl), .funs = funs(xyz = f(., mtcars)))
# This is closer but still it would be better if the function allowed the dataset to be submitted to the function without restating the name of the dataset
f <- function(fld, second){
fld/second
}
mutate_at(mtcars, .vars = vars(mpg, cyl), .funs = funs(xyz = f(., wt)))
library(tidyverse)
f <- function(num, denom) num/denom
mtcars %>%
mutate_at(vars(mpg, cyl), f, denom = quote(wt))
尽管在此特定示例中,不需要自定义函数。
mtcars %>%
mutate_at(vars(mpg, cyl), `/`, quote(wt))
dplyr 1.0.6 的更新版本:
mtcars %>%
mutate(across(c(mpg, cyl), `/`, wt))
也许是这样的?
f <- function(fld,var){
fld/var
}
mtcars %>%
mutate_at(vars(mpg,cyl), .funs = funs(xyz = f(.,wt)))
编辑 (2020-08-24):
由于2020年下半年,随着推出dplyr 1.0.0的, mutate_at
已被取代结合mutate
与across
功能:
mtcars %>%
mutate(across(c(mpg, cyl), ~ f(.,wt), .names = "{col}_xyz"))
为什么不简单
mutate(mtcars, mpg2 = mpg / wt, cyl2 = cyl / wt)
有一个cur_data()
函数将有助于使mutate_at()
调用更加紧凑,因为您不必为应用于每一列的函数指定第二个参数:
f <- function(fld){
fld / cur_data()$wt
}
mutate_at(mtcars, .vars=vars(mpg, cyl), .funs=funs(xyz = f))
附加说明:
cur_data_all()
mutate_at
现在被mutate(.data, across())
mutate_at
mutate(.data, across())
取代,所以最好这样做mtcars %>% mutate(across(.cols=c(mpg, cyl), .fns=f, .names='{.col}_xyz'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.