I would like to count the occurrences of a string in a column ....per group. In this case the string is often a substring in a character column.
I have some data e.g.
ID String village
1 fd_sec, ht_rm, A
2 NA, ht_rm A
3 fd_sec, B
4 san, ht_rm, C
The code that I began with is obviously incorrect, but I am failing on my search to find out I could use the grep function in a column and group by village
impacts <- se %>% group_by(village) %>%
summarise(c_NA = round(sum(sub$en41_1 == "NA")),
c_ht_rm = round(sum(sub$en41_1 == "ht_rm")),
c_san = round(sum(sub$en41_1 == "san")),
c_fd_sec = round(sum(sub$en41_1 == "fd_sec")))
Ideally my output would be:
village fd_sec NA ht_rm san
A 1 1 2
B 1
C 1 1
Thank you in advance