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

后端 未结 3 653
别跟我提以往
别跟我提以往 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 15:58

    ( 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
    

提交回复
热议问题