Reputation: 89
My data looks something like this:
df <- c("I am a car","I will","I have","I give","A bat","A cat")
df <- as.data.frame(df)
colnames(df) <- c("text")
df$count <- str_count(df$text, regex("a{1}?",ignore_case = T))
I want to count just the one instance of 'a' in the first row, not every time that it appears in the whole string. Thanks!
Upvotes: 1
Views: 161
Reputation: 887213
Perhaps we need grep
as.integer(grepl("\\ba\\b", df$text, ignore.case=TRUE))
Or using stringr
library(stringr)
as.integer(str_detect(df$text, "\\ba\\b"))
#[1] 1 0 1 0 1 1
Upvotes: 2