Count Number of Occurrences of Certain Character in String in R - GeeksforGeeks

16 May, 2021

In this article, we will discuss how to count the number of occurrences of a certain character in String in R Programming Language.
The stringR package in R is used to perform string manipulations. It needs to be explicitly installed in the working space to access its methods and routines. 
install.packages("stringr")
The stringr package provides a str_count() method which is used to count the number of occurrences of a certain pattern specified as an argument to the function. The pattern may be a single character or a group of characters. Any instances matching to the expression result in the increment of the count. This method can also be invoked over a vector of strings, and an individual count vector is returned containing individual counts of the number of pattern matches found. However, this method is only considered approximate of regex matching. In case, no matches are found 0 is returned. 
Syntax: str_count(str, pattern = “”)
Parameters : 
str – The string to count the occurrences of
pattern – the pattern to match to
Example 1:
R
 

 
 










library(stringr) 
  
# declaring string 
str1 = "$geeks%for!geeks%"
  
# declaring character to find 
ch1 = "a"
print("Count for character a") 
str_count(str1,ch1) 
  
ch2 = "%"
print("Count for character %") 
str_count(str1,ch2) 






Output
[1] "Count for character a" 
[1] 0 
[1] "Count for character %" 
[1] 2
Example 2:
R
 

 
 










library(stringr) 
  
# declaring string 
str = c("$geeks%for!geeks%","cs^e%portal", 
        "le%..e3oten","joinnow3") 
print ("Original vector") 
print (str) 
  
# declaring character to find 
ch2 = "%"
print("Count for character %") 
str_count(str,ch2) 






Output
[1] "Original vector"
[1] "$geeks%for!geeks%" "cs^e%portal" "le%..e3oten" "joinnow3"
[1] "Count for character %"
[1] 2 1 1 0
This approach uses a variety of methods available in base R to compute the number of occurrences of a specific character in R. The gregexpr() method is used to return a list of sublists that match a specific pattern of the argument list of the function. The pattern matching used is case-sensitive in this case. 
Syntax:
gregexpr(pattern, text)
This is followed by the application of the regmatches() method in R which is used to extract and then replace the list of matched substrings returned by gregexpr() method. The first argument is the original vector and the second argument is the object returned as a result of the previous method. The lengths() method is then applied in order to return the individual lengths of all the elements of the argument vector. 
Syntax:
lengths(x)
Example 1:
R
 

 
 










# declaring string 
str1 = "$geeks%for!geeks%"
  
# declaring character to find 
ch1 = "a"
print("Count for character a") 
lengths(regmatches(str1, gregexpr(ch1, str1))) 
  
ch2 = "%"
print("Count for character %") 
lengths(regmatches(str1, gregexpr(ch2, str1))) 






Output
[1] "Count for character a"
[1] 0
[1] "Count for character %"
[1] 2
Example 2:
R
 

 
 










# declaring string 
str = c("$geeks%for!geeks%","cs^e%portal","le%..e3oten","joinnow3") 
print ("Original vector") 
print (str) 
  
# declaring character to find 
ch2 = "%"
print("Count for character %") 
lengths(regmatches(str, gregexpr(ch2, str))) 






Output
[1] "Original vector"
[1] "$geeks%for!geeks%" "cs^e%portal" "le%..e3oten" "joinnow3"
[1] "Count for character %"
[1] 2 1 1 0

Picked


R String-Programs


R-strings


R Language


R Programs

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
Data Visualization in R
Control Statements in R Programming
Change Color of Bars in Barchart using ggplot2 in R
Loops in R (for, while, repeat)
Logistic Regression in R Programming
How to change Row Names of DataFrame in R ?
Remove rows with NA in one column of R DataFrame
How to filter R DataFrame by values in a column?
Merge DataFrames by Column Names in R
Convert Matrix to Dataframe in R