Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count the number of occurrences of "(" in a string

Tags:

r

stringr

I am trying to get the number of open brackets in a character string in R. I am using the str_count function from the stringr package

s<- "(hi),(bye),(hi)"
str_count(s,"(")

Error in stri_count_regex(string, pattern, opts_regex = attr(pattern, : ` Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)

I am hoping to get 3 for this example

like image 389
NPHARD Avatar asked Feb 22 '17 14:02

NPHARD


People also ask

How do you count the number of occurrences of an element in a string?

count() One of the built-in ways in which you can use Python to count the number of occurrences in a string is using the built-in string . count() method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method.

How do you count a string occurrence in a string?

First, we split the string by spaces in a. Then, take a variable count = 0 and in every true condition we increment the count by 1. Now run a loop at 0 to length of string and check if our string is equal to the word.

How do you count occurrences of each character in a string?

Declare a Hashmap in Java of {char, int}. Traverse in the string, check if the Hashmap already contains the traversed character or not. If it is present, then increase its count using get() and put() function in Hashmap. Once the traversal is completed, traverse in the Hashmap and print the character and its frequency.


2 Answers

( is a special character. You need to escape it:

str_count(s,"\\(")
# [1] 3

Alternatively, given that you're using stringr, you can use the coll function:

str_count(s,coll("("))
# [1] 3
like image 115
sebastian-c Avatar answered Sep 22 '22 19:09

sebastian-c


You could also use gregexpr along with length in base R:

sum(gregexpr("(", s, fixed=TRUE)[[1]] > 0)
[1] 3

gregexpr takes in a character vector and returns a list with the starting positions of each match. I added fixed=TRUE in order to match literals.length will not work because gregexpr returns -1 when a subexpression is not found.


If you have a character vector of length greater than one, you would need to feed the result to sapply:

# new example
s<- c("(hi),(bye),(hi)", "this (that) other", "what")
sapply((gregexpr("(", s, fixed=TRUE)), function(i) sum(i > 0))
[1] 3 1 0
like image 28
lmo Avatar answered Sep 24 '22 19:09

lmo