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.
You can also use cSplit()
from my "splitstackshape" package. Since this package also loads "data.table", you can then just use dcast()
to tabulate the result.
Example:
library(splitstackshape)
cSplit(mydf, "String", direction = "long")[, dcast(.SD, village ~ String)]
# Using 'village' as value column. Use 'value.var' to override
# Aggregate function missing, defaulting to 'length'
# village fd_sec ht_rm san NA
# 1: A 1 2 0 1
# 2: B 1 0 0 0
# 3: C 0 1 1 0