如何使用 Data.Table 代码上的 Tidy Evaluation

how to use the tidy evaluation on data.table code

提问人:jkatam 提问时间:11/17/2023 最后编辑:ThomasIsCodingjkatam 更新时间:11/17/2023 访问量:41

问:

我有以下数据

行之有效的Tidyverse方法

df <- data.frame(
  name=c('Mawan','Garang','Ayen','Alien'),
  birth_day=c('2020-11-23','1981-05-24','2003-02-18','N/A')
)

这是获取年龄(years_of_birth)的函数

age_tidy <- function(dfs, birthdate, refdate){
dfs %>% mutate(years_of_birth=ifelse(is.na({{birthdate}}), NA, 
                                    round(({{refdate}}-as.Date({{birthdate}}))/365.25,digits=2 )))

}

age_tidy(df, birth_day, lubridate::today())

data.table 代码不适用于整洁评估,请解释如何将其转换为整洁评估


age_dt <- function(dfs, birthdate, refdate){
  browser()
dfdt <- data.table::setDT(dfs)
dfdt <- eval_tidy(quo(dfdt[,years_of_birth:=ifelse(is.na({{birthdate}}), NA, 
                              round(({{refdate}}-as.Date({{birthdate}}))/365.25,digits=2 ))]))
return(dfdt)
}

age_dt(df, birth_day, lubridate::today())
r

评论


答: 暂无答案