I am having a data frame
df <- data.frame(
"Quarter" = c("Q1 2019","Q1 2019","Q1 2019","Q2 2019","Q2 2019","Q2 2019","Q2 2019","Q3 2019","Q3 2019","Q3 2019","Q3 2019","Q4 2019","Q4 2019"),
"Name" = c("Ram","John","Jack","Ram","Rach","Will","John","Ram","Rach","Will","John","Rach","John"),
stringsAsFactors = FALSE
)
I need to calculate the number of persons who were added and left in each quarter by comparing it with the previous quarter.
The expected output is
quarterYear status Count
1 Q1 2019 Added 3
2 Q1 2019 Left 0
3 Q2 2019 Added 2
4 Q2 2019 Left 1
5 Q3 2019 Added 0
6 Q3 2019 Left 0
7 Q4 2019 Added 0
8 Q4 2019 Left 2
I am not sure of how to compare two groups and get the count.
How can I achieve the expected output in R?