Sergio Suarez
Sergio Suarez

Reputation: 117

R count the number of words starts with given letter in a phrase

i would like to get the count times that in a given string a word start with the letter given.

For example, in that phrase: "that pattern is great but pigs likes milk" if i want to find the number of words starting with "g" there is only 1 "great", but right now i get 2 "great" and "pigs".

this is the code i use:

x <- "that pattern is great but pogintless"
sapply(regmatches(x, gregexpr("g", x)), length)

Upvotes: 3

Views: 1277

Answers (2)

Antonios
Antonios

Reputation: 1939

You can also do it with stringr library

library(stringr)
str_count(str_split(x," "),"\\bg")

Upvotes: 0

akrun
akrun

Reputation: 887088

We need either a space or word boundary to avoid th letter from matching to characters other than the start of the word. In addition, it may be better to use ignore.case = TRUE as some words may begin with uppercase

lengths(regmatches(x, gregexpr("\\bg", x, ignore.case = TRUE)))

The above can be wrapped as a function

fLength <- function(str1, pat){
       lengths(regmatches(str1, gregexpr(paste0("\\b", pat), str1, ignore.case = TRUE)))
 }

fLength(x, "g")
#[1] 1

Upvotes: 5

Related Questions