簡體   English   中英

將 dplyr mutate_at 與自定義函數一起使用

[英]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已被取代結合mutateacross功能:

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))

附加說明:

  1. 如果您需要該函數來引用分組變量,請使用cur_data_all()
  2. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM