【发布时间】:2021-12-01 09:34:48
【问题描述】:
我整理了以下功能。它一直工作到最后一部分(在代码中的注释中注明),它必须将对象连接在一起。我不知道如何让它工作。我相信我的主要问题与将 colName 参数转换为 joiner 函数的“by =”参数的字符串有关。关于 group_by 函数,我不确定我放在大括号中的内容是否有效。如果有人可以提供帮助,那就太好了!
emp_turnover_fun <- function(data, colName, year = "2015") {
# Convert colName to symbol or check if symbol
colName <- ensym(colName)
# Terminations by year and variable in df
term_test <- data %>%
filter(year(DateofTermination) == year) %>%
count(!!(colName)) %>%
clean_names()
# Start employees by var and year
fun_year_job <- paste(year, "-01-01", sep = "")
start_test <- data %>%
select(DateofHire, DateofTermination, !!(colName)) %>%
filter(
DateofHire <= fun_year_job,
DateofTermination > fun_year_job | is.na(DateofTermination)
) %>%
count(!!(colName))
# End employees by year and var
year_pos <- year %>% as.character()
year_num_plus_pos <- as.character(as.numeric(year_pos) + 1)
fun_year2_pos <- paste(year_num_plus_pos, "-01-01", sep = "")
end_test <- data %>%
select(DateofHire, DateofTermination, !!(colName)) %>%
filter(
DateofHire <= fun_year2_pos,
DateofTermination > fun_year2_pos | is.na(DateofTermination)
) %>%
count(!!(colName))
#### PROBLEM BEGINS HERE
join_turnover_year <- full_join(start_test, end_test, by = str(colName)) %>%
full_join(y = term_test, by = str(colName)) %>%
setNames(c(str(colName), "Start_Headcount", "End_Headcount", "Terminations")) %>%
group_by({{colName}}) %>%
summarise(Turnover = ((Terminations) / (Start_Headcount + End_Headcount)) * 100)
return(join_turnover_year)
}
【问题讨论】:
标签: r dplyr tidyverse tidyeval