R count number of commas and string

前端 未结 4 937
遇见更好的自我
遇见更好的自我 2021-01-17 19:25

I have a string:

    str1 <- \"This is a string, that I\'ve written 
        to ask about a question, or at least tried to.\"

How would

4条回答
  •  感情败类
    2021-01-17 19:49

    This really is an adaptation of Richie Cotton's answer. I hate having to repeat the same function over and over. This approach allows you to feed a vector of terms to match within the string:

    str1 <- "This is a string, that I've written to ask about a question, 
        or at least tried to."
    matches <- c(",", "ion") 
    sapply(matches,  function(x) length(gregexpr(x, str1, fixed = TRUE)[[1]]))
    #  , ion 
    #  2   1 
    

提交回复
热议问题