如何在函数中为输出全局数据框分配名称?

时间:2019-01-03 17:41:45

标签: r dplyr

在最终代码中将它们实现为数据表之前,我尝试清理大约12个数据集。目前,我有适用于各个数据集的代码,但我想将此功能实现为功能,因此不必一遍又一遍地重新输入代码块。

countyshapesClean <- function(inputdata, outputdata) {
  outputdata <<- inputdata %>%
select(
  county_cl,
  state_abb,
  avg_opioid,
  avg_oxy_hydro,
  avg_opioid_perc,
  avg_oxy_hydro_perc,
  avg_opioid_ppp,
  avg_oxy_hydro_ppp
) %>% 
mutate(
  avg_opioid = format(round(as.numeric(avg_opioid),2), nsmall = 2, big.mark = ","),
  avg_oxy_hydro = format(round(as.numeric(avg_oxy_hydro),2), nsmall = 2, big.mark = ","),
  avg_opioid_perc = format(round(as.numeric(avg_opioid_perc),2), nsmall = 2, big.mark = ","),
  avg_oxy_hydro_perc = format(round(as.numeric(avg_oxy_hydro_perc),2), nsmall = 2, big.mark = ","),
  avg_opioid_ppp = format(round(as.numeric(avg_opioid_ppp),2), nsmall = 2, big.mark = ","),
  avg_oxy_hydro_ppp = format(round(as.numeric(avg_oxy_hydro_ppp),2), nsmall = 2, big.mark = ",")
)
}

countyshapesClean(inputdata = countymerge2006, outputdata = countymerge2006clean)

这可行,但是我的输出数据集名为outputdata,而不是countymerge2006clean。我将如何更改此代码以获得所需的结果?

1 个答案:

答案 0 :(得分:2)

请勿将outputdata传递给函数。相反,要做类似的事情

countymerge2006clean <- countyshapesClean(inputdata = countymerge2006)

函数在其中返回更新的data.frame。

countyshapesClean <- function(inputdata) {
  inputdata %>%
  select(...) %>% 
  mutate(...)
}

R中的适当函数不应在其自身范围之外创建变量。 (使用<<-被认为不是好习惯。)

当然,R几乎是不可能的,您可以使用assign()函数在函数范围之外创建变量(尽管我强烈建议您不要这样做)。看起来像这样

countyshapesClean <- function(inputdata, outputdata) {
  outvar <- rlang::ensym(outputdata)
  new <- inputdata %>%
    select(...) %>% 
    mutate(...)
  assign(rlang::quo_name(outvar), new, parent.frame())
}
countyshapesClean(inputdata = countymerge2006, outputdata = countymerge2006clean)