【发布时间】:2021-05-22 06:25:51
【问题描述】:
该函数本身可以工作,但我正在尝试遍历数据中的所有列,我知道使用 ~apply() 可以工作,并且希望您能帮助使用它,谢谢。
library(scales)
factorVars <- select_if(df, is.factor)
## function to tidy up to a summary table
tidy_eval_arrange <- function(.data, var) {
tidydata <- .data %>%
count({{var}}) %>%
mutate(pct = n / sum(n),
pctlabel = paste0(round(pct * 100), "%"))
print(tidydata)
}
## The issue is here, prints without calculating the percentage properly.
nm <- names(factorVars)
for(i in seq_along(nm)){
tidy_eval_arrange(df, nm[i])
}
【问题讨论】:
-
什么是
df?您能否提供一些示例数据,以便我们reproduce您的问题? -
嗨@Joe Roe,df 是我们要测试的数据框。
标签: r function for-loop apply summarize