I have a dataframe with multiple subjects (company), year, personalname and gender (Female,Male). I want to obtain the year personalname changes (if there is a change). Also, if a change occurs during that specific year, I would like to create two binary variables: "FemaletoMale" (and MaletoFemale) indicating that the change occurs from female to male (or male to female).
So, if I had a table like
companyid year personalname gender
1 1990 Alison Female
1 1991 Alison Female
1 1992 Kate Female
1 1993 Kate Female
2 1990 George Male
2 1991 Kate Female
2 1992 Kate Female
3 1990 Michael Male
3 1991 Dwight Male
I am aware of that question that helps me to count the number of changes: How to tell if a value changed over dimensions in R
df<- df %>% group_by(companyid) %>% summarise(ChangeYear = sprintf("%s to %s", min(year), max(year)), change.count = length(unique(personalname)) - 1)
This gives me the number of changes. What I wanted to see is;
companyid change.count changeyear FemaletoMale MaletoFemale
1 1 1992 0 0
2 1 1991 0 1
3 1 1991 0 0