I am having the hardest time coming up with a code that lets me match a topic (Column B) to a name (Column A) and create a frequency column for the times B has matched with A (or how many times both have appeared together). Col A and B are codes for longer names.
I thought maybe using the count function from plyr
but cant make it work. Maybe you can give me an idea of what I could use for a code?
For example I have a table:
**Col A | Col B** |
---|---|
1 | 38 |
1 | 6 |
1 | 38 |
2 | 38 |
2 | 7 |
2 | 7 |
2 | 8 |
2 | 7 |
The result that I am looking for is
**Col A | Col B | freq** |
---|---|---|
1 | 38 | 2 |
1 | 6 | 1 |
2 | 38 | 1 |
2 | 7 | 3 |
2 | 8 | 1 |
So the number 38 has appeared in "1" two times. 6 has appeared one time. and so on. I have 600 rows of data and cant come up with a useful or even a close call code. Thank you so much for your help!
Summarise and count using dplyr:
library(dplyr)
df2 <- df %>%
group_by(col1, col2) %>%
summarise(count = n()) %>%
ungroup()
returns:
col1 col2 count
<dbl> <dbl> <int>
1 1 6 1
2 1 38 2
3 2 7 3
4 2 8 1
5 2 38 1