Reputation: 99
Suppose I have a data frame where each person is represented as having a specific dose category (1-4) and specific followup category (1-7).
dose_followup = data.frame(
dose = c(3,1,2,2,4,3,1,4,1,3,3,2,4,1,3,3),
followup = c(2,4,2,1,6,2,7,3,6,3,5,5,6,4,2,2))
How can I count the number of co-occurrences and store them? For example, dose == 3 occurs with followup == 2 a total of 4 times. I want to make that count (i.e., 4) and either print it or store the value 4 in a matrix's cell corresponding to dose == 3 and followup == 2.
My actual dataframe is large, but it has only 4 dose categories and 7 followup categories. So the output matrix would be small.
I have tried various approaches with group_by() and with summarise() but without success. Thank you for any help.
Upvotes: 0
Views: 31
Reputation: 2930
A simple table
will do it:
table(dose_followup)
Result:
followup
dose 1 2 3 4 5 6 7
1 0 0 0 2 0 1 1
2 1 1 0 0 1 0 0
3 0 4 1 0 1 0 0
4 0 0 1 0 0 2 0
If you insist doing it with tidyverse
you can use
dose_followup %>% group_by(dose, followup) %>% summarise(n())
Result:
1.00 4.00 2
1.00 6.00 1
1.00 7.00 1
2.00 1.00 1
2.00 2.00 1
2.00 5.00 1
3.00 2.00 4
3.00 3.00 1
3.00 5.00 1
4.00 3.00 1
4.00 6.00 2
Upvotes: 1