我自己做了如下的功能:
emp_term_var <- function(data, colName, year = "2015") {
# Terminations by year and variable in df
colName <- enquo(colName)
term_test <- data %>%
filter(year(DateofTermination) == year) %>%
group_by(UQ(colName)) %>%
count(UQ(colName)) %>%
clean_names()
return(term_test)
}
Run Code Online (Sandbox Code Playgroud)
我有一个包含多个列的 df,例如 Department、State、Position 等。当我想使用我编写的函数时,我将列的名称不带引号,如下所示:
emp_term_var(data = df, colName = Department, year = "2015")
Run Code Online (Sandbox Code Playgroud)
返回:
# A tibble: 5 x 2
# Groups: department [5]
department n
<chr> <int>
1 Admin Offices 1
2 IT/IS 4
3 Production 15
4 Sales 1
5 Software Engineering 2 …Run Code Online (Sandbox Code Playgroud)