I am wanting to count the frequencies of certain strings within a dataframe.
strings <- c("pi","pie","piece","pin","pinned","post")
df <- as.data.frame(strings)
I would then like to count the frequency of the strings:
counts <- c("pi", "in", "pie", "ie")
To give me something like:
string freq
pi 5
in 2
pie 2
ie 2
I have experimented with grepl
and table
but I don't see how I can specify the strings I want to search for are.
You can use
sapply()
to go thecounts
and match every item incounts
against thestrings
column indf
usinggrepl()
this will return alogical
vector (TRUE
if match,FALSE
if non-match). You can sum this vector up to get the number of matches.This will return:
Frequency table created by
qgrams
from thestringdist
packageYou can use
adist
from base R:If you are comfortable with regular expressions then you can do: