I would like to count in a dataframe how often a certain word occurs in each row. No problem as far as that goes. But I would like to extend the function so that the value does not have to occur EXACTLY. For example, abc should also be counted if the value is abcd.
Example (looking for value "abc"):
ID | Value | Should count |
---|---|---|
1 | adx | false |
2 | abc | true |
3 | abcd | true |
4 | abzc | false |
5 | xabc | true |
6 | xyzabcabczyd | true |
Somehow I'm stuck on how to write a function that returns the desired value here (which would be 4).
Thank you very much!
CodePudding user response:
With the grepl
function you can match the desired pattern. As a result you get a logical vector. Count the occurrences of TRUE
in the vector with sum
and you have your result.
df <- data.frame(
Id = 1:6,
Value = c("adx", "abc", "abcd", "abzc", "xabc", "xyzabcabczyd")
)
sum(grepl("abc", df$Value))
#> [1] 4
Created on 2021-10-10 by the reprex package (v2.0.1)