0

I have created the following function to count the number of terminations in a company by variable in the dataframe.

 emp_term_var <- function(data, colName, year = "2015") {
  colName <- enquo(colName) 
  term_test <- data %>%
    filter(year(DateofTermination) == year) %>%
    group_by(UQ(colName)) %>%
    count(UQ(colName)) %>%
    clean_names()
  return(term_test)
}

I plug in my arguments and decide I want to see the number of terminations by department. The second argument of the function is the name of a column in the dataframe. I could do this for position and managername as well.

emp_term_var(df, Department, year = "2015")

This returns:

# 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

I have data from 2010 to 2018 and would like to map over this function for every year using a vector for the year argument:

years <- c("2010" ,"2011" ,"2012" ,"2013" ,"2014", "2015" ,"2016","2017" ,"2018")

I try to map the function with pmap with the following code:

pmap(list(df, Department, years), emp_term_var)

But I get the following message:

Error in is.data.frame(.l) : object 'Department' not found

How can I fix this?

Taren Shaw
  • 89
  • 6

0 Answers0