count the number of occurrences of “(” in a string

后端 未结 3 662
别跟我提以往
别跟我提以往 2020-12-11 15:33

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



        
3条回答
  •  误落风尘
    2020-12-11 16:05

    If you want to do it in base R you can split into a vector of individual characters and count the "(" directly (without representing it as a regular expression):

    > s<- "(hi),(bye),(hi)"
    > chars <- unlist(strsplit(s,""))
    > length(chars[chars == "("])
    [1] 3
    

提交回复
热议问题