structure(list(Year = c(2005, 2005, 2005, 2005, 2005, 2005, 2005,
2005, 2006, 2006, 2006, 2006, 2006, 2006, 2006), CompanyID = c(5,
5, 5, 6, 6, 7, 7, 7, 5, 5, 5, 6, 6, 7, 7), Age = c(55, 48, 62,
62, 41, 62, 55, 63, 55, 48, 62, 62, 41, 62, 55), Gender = c("M",
"M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "F", "F", "M",
"F"), JOIN = c(1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1),
LEAVE = c(0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -15L))
From the above data frame (which is a small sample of a large dataset)- I would like to filter rows satisfying the following two conditions -
- In each group (
group_by (Year, CompanyID), Each year (Yearvariable) a firm (which isCompanyID) must appoint (JOINvariable) only one female director (GENDERvariable) to replace a departing (LEAVEvariable) male director (GENDERvariable) in the year
AND
- departing male director to be older than 60 (
Agevariable)
The expected output is -
tribble(
~"Year", ~"CompanyID", ~"Age", ~"Gender", ~"JOIN", ~"LEAVE",
2005, 6, 62, 'M', 0, 1,
2005, 6, 41, 'F', 1, 0,
2006, 7, 62, 'M', 0, 1,
2006, 7, 55, 'F', 1, 0
)
Because the expected output satisfies the above two criteria - that each year each company replace a departing male director with a female director and the departing male director age is greater than 60.