Within my data I have a subset of data that look like this:
Incident | Year | Person1 |Person2|
:---- |:---: |:------: | -----:|
1| 2014 | A | B |
2| 2014 | A | |
3| 2016 | B | C |
4| 2018 | A | C |
5| 2020 | C | |
My goal is to count frequencies for each person and graph the data in a barchart (x axis=year, y=frequency, persons grouped). I could do count(data$Person1) and count(data$Person2) and sum them "by hand" to count frequencies, but I will need the data to be combined for the bar chart. I don't think I would use concatenate because I don't want AB, AC, etc.
I tried
group_data<-group_by(.data=data,Incident,Person1,Person2)
sum_counts<-dplyr::summarise(group_data,total.count=n())
View(sum_counts)
But the output is essentially the table above.
Is there an elegant way to group columns Person1 and Person2 without muddling the number of incidents (each row = a distinct incident)? Or in order to make my desired barchart, do I need to totally restructure the data?
Thanks in advance for wisdom.
Data:
dput(srkw.dat) structure(list(incident = 1:78, year = c(1962L, 1976L, 1981L, 1981L, 1982L, 1987L, 1989L, 1990L, 1992L, 1992L, 1992L, 1994L, 1998L, 2003L, 2003L, 2003L, 2003L, 2004L, 2004L, 2004L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 2011L, 2011L, 2013L, 2013L, 2013L, 2014L, 2014L, 2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2018L, 2018L, 2018L, 2019L, 2020L, 2020L, 2020L, 2020L), pod1 = c("L", "L", "L", "L", "L", "L", "L", "K", "L", "L", "L", "L", "L", "J", "L", "L", "J", "L", "L", "L", "J", "J", "J", "J", "J", "L", "L", "J", "L", "L", "J", "J", "K", "L", "L", "J", "L", "L", "K", "L", "L", "L", "J", "J", "J", "L", "L", "K", "J", "L", "K", "", "K", "J", "J", "L", "J", "L", "K", "K", "L", "J", "J", "J", "L", "J", "J", "L", "L", "J", "J", "J", "J", "L", "J", "", "J", "L"), pod2 = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "K", "", "K", "", "", "", "", "K", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "L", "", "", "", "", "", "", "", "", "", "", ""), class = "data.frame", row.names = c(NA, -78L))