How to calculate the number of occurrence of a given character in each row of a column of strings?count number of numbers (not digits) in a stringHow to count the number of a specific digit in each cell in Routput computation in R using shinyCount number of short strings in a long string in RCount Occurrences of Character “?” in rR - count number of items in a piped listSearch for a specific number of characters in a stringHow to find the observations that have more than one underscore?How to find number 1s in 101 in RHow to find multiple underscore in the same string?How to replace all occurrences of a string in JavaScriptHow do I remove all non alphanumeric characters from a string except dash?How to count string occurrence in string?How to drop rows of Pandas DataFrame whose value in certain columns is NaNCount number of ocorrences with regexJava String Matching the RegexFind specific strings, count their frequency in a given text, and report it as a proportion of the number of wordsCalculate the number of occurrence of a given character in each row of a data frame?Extract pattern that occurs multiple times within stringHow to extract index columns and rows in R?

Can a Knock spell open the door to Mordenkainen's Magnificent Mansion?

Walter Rudin's mathematical analysis: theorem 2.43. Why proof can't work under the perfect set is uncountable.

Capacitor electron flow

How do I prevent inappropriate ads from appearing in my game?

categorizing a variable turns it from insignificant to significant

What is this high flying aircraft over Pennsylvania?

What is the period/term used describe Giuseppe Arcimboldo's style of painting?

What is the meaning of "You've never met a graph you didn't like?"

Not hide and seek

I keep switching characters, how do I stop?

Reasons for having MCU pin-states default to pull-up/down out of reset

Is there any common country to visit for persons holding UK and Schengen visas?

Can Gdal.Translate() return an object instead of writing a file?

Should I warn a new PhD Student?

What is the tangent at a sharp point on a curve?

Why does a 97 / 92 key piano exist by Bosendorfer?

C++ lambda syntax

Make a Bowl of Alphabet Soup

Is there a distance limit for minecart tracks?

Why would five hundred and five same as one?

How can a new country break out from a developed country without war?

Why is "la Gestapo" feminine?

Why doesn't Gödel's incompleteness theorem apply to false statements?

New Order #2: Turn My Way



How to calculate the number of occurrence of a given character in each row of a column of strings?


count number of numbers (not digits) in a stringHow to count the number of a specific digit in each cell in Routput computation in R using shinyCount number of short strings in a long string in RCount Occurrences of Character “?” in rR - count number of items in a piped listSearch for a specific number of characters in a stringHow to find the observations that have more than one underscore?How to find number 1s in 101 in RHow to find multiple underscore in the same string?How to replace all occurrences of a string in JavaScriptHow do I remove all non alphanumeric characters from a string except dash?How to count string occurrence in string?How to drop rows of Pandas DataFrame whose value in certain columns is NaNCount number of ocorrences with regexJava String Matching the RegexFind specific strings, count their frequency in a given text, and report it as a proportion of the number of wordsCalculate the number of occurrence of a given character in each row of a data frame?Extract pattern that occurs multiple times within stringHow to extract index columns and rows in R?













80















I have a data.frame in which certain variables contain a text string. I wish to count the number of occurrences of a given character in each individual string.



Example:



q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"))


I wish to create a new column for q.data with the number of occurence of "a" in string (ie. c(2,1,0)).



The only convoluted approach I have managed is:



string.counter<-function(strings, pattern) 
counts<-NULL
for(i in 1:length(strings))
counts[i]<-length(attr(gregexpr(pattern,strings[i])[[1]], "match.length")[attr(gregexpr(pattern,strings[i])[[1]], "match.length")>0])

return(counts)


string.counter(strings=q.data$string, pattern="a")

number string number.of.a
1 1 greatgreat 2
2 2 magic 1
3 3 not 0









share|improve this question




























    80















    I have a data.frame in which certain variables contain a text string. I wish to count the number of occurrences of a given character in each individual string.



    Example:



    q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"))


    I wish to create a new column for q.data with the number of occurence of "a" in string (ie. c(2,1,0)).



    The only convoluted approach I have managed is:



    string.counter<-function(strings, pattern) 
    counts<-NULL
    for(i in 1:length(strings))
    counts[i]<-length(attr(gregexpr(pattern,strings[i])[[1]], "match.length")[attr(gregexpr(pattern,strings[i])[[1]], "match.length")>0])

    return(counts)


    string.counter(strings=q.data$string, pattern="a")

    number string number.of.a
    1 1 greatgreat 2
    2 2 magic 1
    3 3 not 0









    share|improve this question


























      80












      80








      80


      20






      I have a data.frame in which certain variables contain a text string. I wish to count the number of occurrences of a given character in each individual string.



      Example:



      q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"))


      I wish to create a new column for q.data with the number of occurence of "a" in string (ie. c(2,1,0)).



      The only convoluted approach I have managed is:



      string.counter<-function(strings, pattern) 
      counts<-NULL
      for(i in 1:length(strings))
      counts[i]<-length(attr(gregexpr(pattern,strings[i])[[1]], "match.length")[attr(gregexpr(pattern,strings[i])[[1]], "match.length")>0])

      return(counts)


      string.counter(strings=q.data$string, pattern="a")

      number string number.of.a
      1 1 greatgreat 2
      2 2 magic 1
      3 3 not 0









      share|improve this question
















      I have a data.frame in which certain variables contain a text string. I wish to count the number of occurrences of a given character in each individual string.



      Example:



      q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"))


      I wish to create a new column for q.data with the number of occurence of "a" in string (ie. c(2,1,0)).



      The only convoluted approach I have managed is:



      string.counter<-function(strings, pattern) 
      counts<-NULL
      for(i in 1:length(strings))
      counts[i]<-length(attr(gregexpr(pattern,strings[i])[[1]], "match.length")[attr(gregexpr(pattern,strings[i])[[1]], "match.length")>0])

      return(counts)


      string.counter(strings=q.data$string, pattern="a")

      number string number.of.a
      1 1 greatgreat 2
      2 2 magic 1
      3 3 not 0






      regex r dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 14 '12 at 15:26







      Etienne Low-Décarie

















      asked Sep 14 '12 at 15:17









      Etienne Low-DécarieEtienne Low-Décarie

      6,265145182




      6,265145182






















          11 Answers
          11






          active

          oldest

          votes


















          104














          The stringr package provides the str_count function which seems to do what you're interested in



          # Load your example data
          q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = F)
          library(stringr)

          # Count the number of 'a's in each element of string
          q.data$number.of.a <- str_count(q.data$string, "a")
          q.data
          # number string number.of.a
          #1 1 greatgreat 2
          #2 2 magic 1
          #3 3 not 0





          share|improve this answer


















          • 1





            Yours was much faster although it does need an as.character() around the main argument to succeed with the problem posed.

            – 42-
            Sep 14 '12 at 20:09







          • 1





            @DWin - That's true but I avoided that issue by adding stringsAsFactors = FALSE when defining the data frame.

            – Dason
            Sep 14 '12 at 20:10











          • Sorry I was unclear. I was actually responding to tim riffe and telling him that his function threw an error with the problem posed. He may have used your redefinition of the problem but he didn't say so.

            – 42-
            Sep 14 '12 at 20:14












          • yeah, I also did, stringsAsFactors=TRUE on my comp, but didn't mention this

            – tim riffe
            Sep 14 '12 at 20:31











          • Searching for a string in a factor will work i.e. str_count(d$factor_column,'A') but not vice-versa

            – Nitro
            Sep 27 '17 at 19:24



















          48














          If you don't want to leave base R, here's a fairly succinct and expressive possibility:



          x <- q.data$string
          lengths(regmatches(x, gregexpr("a", x)))
          # [1] 2 1 0





          share|improve this answer




















          • 2





            OK -- maybe that will only feel expressive once you've used the regmatches and gregexpr together a few times, but that combo is powerful enough that I thought it deserved a plug.

            – Josh O'Brien
            Sep 14 '12 at 15:48











          • +1 for regmatches, I was not aware of the function.

            – Roman Luštrik
            Sep 14 '12 at 16:06











          • regmatches is relatively new. It was introduced in 2.14.

            – Dason
            Sep 15 '12 at 17:49











          • I don't think you need the regmatches bit. The function gregexpr returns a list with the indices of the matched occurrences for each element of x.

            – savagent
            Aug 26 '14 at 3:27






          • 1





            Sorry, I forgot about the -1. It only works if each line has at least one match, sapply(gregexpr("g", q.data$string), length).

            – savagent
            Aug 26 '14 at 4:42


















          12














          nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string))
          [1] 2 1 0


          Notice that I coerce the factor variable to character, before passing to nchar. The regex functions appear to do that internally.



          Here's benchmark results (with a scaled up size of the test to 3000 rows)



           q.data<-q.data[rep(1:NROW(q.data), 1000),]
          str(q.data)
          'data.frame': 3000 obs. of 3 variables:
          $ number : int 1 2 3 1 2 3 1 2 3 1 ...
          $ string : Factor w/ 3 levels "greatgreat","magic",..: 1 2 3 1 2 3 1 2 3 1 ...
          $ number.of.a: int 2 1 0 2 1 0 2 1 0 2 ...

          benchmark( Dason = q.data$number.of.a <- str_count(as.character(q.data$string), "a") ,
          Tim = resT <- sapply(as.character(q.data$string), function(x, letter = "a")
          sum(unlist(strsplit(x, split = "")) == letter) ) ,

          DWin = resW <- nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string)),
          Josh = x <- sapply(regmatches(q.data$string, gregexpr("g",q.data$string )), length), replications=100)
          #-----------------------
          test replications elapsed relative user.self sys.self user.child sys.child
          1 Dason 100 4.173 9.959427 2.985 1.204 0 0
          3 DWin 100 0.419 1.000000 0.417 0.003 0 0
          4 Josh 100 18.635 44.474940 17.883 0.827 0 0
          2 Tim 100 3.705 8.842482 3.646 0.072 0 0





          share|improve this answer




















          • 1





            This is the fastest solution in the answers but is made ~30% faster on your benchmark by passing the optional fixed=TRUE to gsub. There are also cases where fixed=TRUE would be required (i.e., when the character you want to count could be interpreted as a regex assertion such as .).

            – C8H10N4O2
            Jan 16 '18 at 16:15



















          5














          sum(charToRaw("abc.d.aa") == charToRaw('.'))


          is an good option.






          share|improve this answer
































            2














            I'm sure someone can do better, but this works:



            sapply(as.character(q.data$string), function(x, letter = "a")
            sum(unlist(strsplit(x, split = "")) == letter)
            )
            greatgreat magic not
            2 1 0


            or in a function:



            countLetter <- function(charvec, letter)
            sapply(charvec, function(x, letter)
            sum(unlist(strsplit(x, split = "")) == letter)
            , letter = letter)

            countLetter(as.character(q.data$string),"a")





            share|improve this answer

























            • I seem to get an error with the first one ... and the second one... (was trying to benchmark all of these.)

              – 42-
              Sep 14 '12 at 20:04












            • Use as.character()

              – 42-
              Sep 14 '12 at 20:10











            • edited to reflect this, thx

              – tim riffe
              Sep 14 '12 at 20:31


















            0














            I count characters same way as Amarjeet. However I prefer to do it in a single line.



            HowManySpaces<-nchar(DF$string)-nchar(gsub(" ","",DF$string)) # count spaces in DF$string





            share|improve this answer
































              0














              The easiest and the cleanest way IMHO is :



              q.data$number.of.a <- lengths(gregexpr('a', q.data$string))

              # number string number.of.a`
              #1 1 greatgreat 2`
              #2 2 magic 1`
              #3 3 not 0`





              share|improve this answer






























                0














                You could just use string division



                require(roperators)
                my_strings <- c('apple', banana', 'pear', 'melon')
                my_strings %s/% 'a'


                Which will give you 1, 3, 1, 0. You can also use string division with regular expressions and whole words.






                share|improve this answer






























                  0














                  The stringi package provides the functions stri_count and stri_count_fixed which are very fast.



                  stringi::stri_count(q.data$string, fixed = "a")
                  # [1] 2 1 0


                  benchmark



                  Compared to the fastest approach from @42-'s answer and to the equivalent function from the stringr package for a vector with 30.000 elements.



                  library(microbenchmark)

                  benchmark <- microbenchmark(
                  stringi = stringi::stri_count(test.data$string, fixed = "a"),
                  baseR = nchar(test.data$string) - nchar(gsub("a", "", test.data$string, fixed = TRUE)),
                  stringr = str_count(test.data$string, "a")
                  )

                  autoplot(benchmark)


                  data



                  q.data <- data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = FALSE)
                  test.data <- q.data[rep(1:NROW(q.data), 10000),]


                  enter image description here






                  share|improve this answer
































                    0














                    The question below has been moved here, but it seems this page doesn't directly answer to Farah El's question.
                    How to find number 1s in 101 in R



                    So, I'll write an answer here, just in case.



                    library(magrittr)
                    n %>% # n is a number you'd like to inspect
                    as.character() %>%
                    str_count(pattern = "1")


                    https://stackoverflow.com/users/8931457/farah-el






                    share|improve this answer






























                      -1














                      s <- "aababacababaaathhhhhslsls jsjsjjsaa ghhaalll"
                      p <- "a"
                      s2 <- gsub(p,"",s)
                      numOcc <- nchar(s) - nchar(s2)


                      May not be the efficient one but solve my purpose.






                      share|improve this answer


















                      • 2





                        This is exactly same as this answer.

                        – zx8754
                        Apr 6 '16 at 11:29










                      Your Answer






                      StackExchange.ifUsing("editor", function ()
                      StackExchange.using("externalEditor", function ()
                      StackExchange.using("snippets", function ()
                      StackExchange.snippets.init();
                      );
                      );
                      , "code-snippets");

                      StackExchange.ready(function()
                      var channelOptions =
                      tags: "".split(" "),
                      id: "1"
                      ;
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function()
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled)
                      StackExchange.using("snippets", function()
                      createEditor();
                      );

                      else
                      createEditor();

                      );

                      function createEditor()
                      StackExchange.prepareEditor(
                      heartbeatType: 'answer',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader:
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      ,
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      );



                      );













                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12427385%2fhow-to-calculate-the-number-of-occurrence-of-a-given-character-in-each-row-of-a%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown

























                      11 Answers
                      11






                      active

                      oldest

                      votes








                      11 Answers
                      11






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      104














                      The stringr package provides the str_count function which seems to do what you're interested in



                      # Load your example data
                      q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = F)
                      library(stringr)

                      # Count the number of 'a's in each element of string
                      q.data$number.of.a <- str_count(q.data$string, "a")
                      q.data
                      # number string number.of.a
                      #1 1 greatgreat 2
                      #2 2 magic 1
                      #3 3 not 0





                      share|improve this answer


















                      • 1





                        Yours was much faster although it does need an as.character() around the main argument to succeed with the problem posed.

                        – 42-
                        Sep 14 '12 at 20:09







                      • 1





                        @DWin - That's true but I avoided that issue by adding stringsAsFactors = FALSE when defining the data frame.

                        – Dason
                        Sep 14 '12 at 20:10











                      • Sorry I was unclear. I was actually responding to tim riffe and telling him that his function threw an error with the problem posed. He may have used your redefinition of the problem but he didn't say so.

                        – 42-
                        Sep 14 '12 at 20:14












                      • yeah, I also did, stringsAsFactors=TRUE on my comp, but didn't mention this

                        – tim riffe
                        Sep 14 '12 at 20:31











                      • Searching for a string in a factor will work i.e. str_count(d$factor_column,'A') but not vice-versa

                        – Nitro
                        Sep 27 '17 at 19:24
















                      104














                      The stringr package provides the str_count function which seems to do what you're interested in



                      # Load your example data
                      q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = F)
                      library(stringr)

                      # Count the number of 'a's in each element of string
                      q.data$number.of.a <- str_count(q.data$string, "a")
                      q.data
                      # number string number.of.a
                      #1 1 greatgreat 2
                      #2 2 magic 1
                      #3 3 not 0





                      share|improve this answer


















                      • 1





                        Yours was much faster although it does need an as.character() around the main argument to succeed with the problem posed.

                        – 42-
                        Sep 14 '12 at 20:09







                      • 1





                        @DWin - That's true but I avoided that issue by adding stringsAsFactors = FALSE when defining the data frame.

                        – Dason
                        Sep 14 '12 at 20:10











                      • Sorry I was unclear. I was actually responding to tim riffe and telling him that his function threw an error with the problem posed. He may have used your redefinition of the problem but he didn't say so.

                        – 42-
                        Sep 14 '12 at 20:14












                      • yeah, I also did, stringsAsFactors=TRUE on my comp, but didn't mention this

                        – tim riffe
                        Sep 14 '12 at 20:31











                      • Searching for a string in a factor will work i.e. str_count(d$factor_column,'A') but not vice-versa

                        – Nitro
                        Sep 27 '17 at 19:24














                      104












                      104








                      104







                      The stringr package provides the str_count function which seems to do what you're interested in



                      # Load your example data
                      q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = F)
                      library(stringr)

                      # Count the number of 'a's in each element of string
                      q.data$number.of.a <- str_count(q.data$string, "a")
                      q.data
                      # number string number.of.a
                      #1 1 greatgreat 2
                      #2 2 magic 1
                      #3 3 not 0





                      share|improve this answer













                      The stringr package provides the str_count function which seems to do what you're interested in



                      # Load your example data
                      q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = F)
                      library(stringr)

                      # Count the number of 'a's in each element of string
                      q.data$number.of.a <- str_count(q.data$string, "a")
                      q.data
                      # number string number.of.a
                      #1 1 greatgreat 2
                      #2 2 magic 1
                      #3 3 not 0






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 14 '12 at 15:25









                      DasonDason

                      45.8k797128




                      45.8k797128







                      • 1





                        Yours was much faster although it does need an as.character() around the main argument to succeed with the problem posed.

                        – 42-
                        Sep 14 '12 at 20:09







                      • 1





                        @DWin - That's true but I avoided that issue by adding stringsAsFactors = FALSE when defining the data frame.

                        – Dason
                        Sep 14 '12 at 20:10











                      • Sorry I was unclear. I was actually responding to tim riffe and telling him that his function threw an error with the problem posed. He may have used your redefinition of the problem but he didn't say so.

                        – 42-
                        Sep 14 '12 at 20:14












                      • yeah, I also did, stringsAsFactors=TRUE on my comp, but didn't mention this

                        – tim riffe
                        Sep 14 '12 at 20:31











                      • Searching for a string in a factor will work i.e. str_count(d$factor_column,'A') but not vice-versa

                        – Nitro
                        Sep 27 '17 at 19:24













                      • 1





                        Yours was much faster although it does need an as.character() around the main argument to succeed with the problem posed.

                        – 42-
                        Sep 14 '12 at 20:09







                      • 1





                        @DWin - That's true but I avoided that issue by adding stringsAsFactors = FALSE when defining the data frame.

                        – Dason
                        Sep 14 '12 at 20:10











                      • Sorry I was unclear. I was actually responding to tim riffe and telling him that his function threw an error with the problem posed. He may have used your redefinition of the problem but he didn't say so.

                        – 42-
                        Sep 14 '12 at 20:14












                      • yeah, I also did, stringsAsFactors=TRUE on my comp, but didn't mention this

                        – tim riffe
                        Sep 14 '12 at 20:31











                      • Searching for a string in a factor will work i.e. str_count(d$factor_column,'A') but not vice-versa

                        – Nitro
                        Sep 27 '17 at 19:24








                      1




                      1





                      Yours was much faster although it does need an as.character() around the main argument to succeed with the problem posed.

                      – 42-
                      Sep 14 '12 at 20:09






                      Yours was much faster although it does need an as.character() around the main argument to succeed with the problem posed.

                      – 42-
                      Sep 14 '12 at 20:09





                      1




                      1





                      @DWin - That's true but I avoided that issue by adding stringsAsFactors = FALSE when defining the data frame.

                      – Dason
                      Sep 14 '12 at 20:10





                      @DWin - That's true but I avoided that issue by adding stringsAsFactors = FALSE when defining the data frame.

                      – Dason
                      Sep 14 '12 at 20:10













                      Sorry I was unclear. I was actually responding to tim riffe and telling him that his function threw an error with the problem posed. He may have used your redefinition of the problem but he didn't say so.

                      – 42-
                      Sep 14 '12 at 20:14






                      Sorry I was unclear. I was actually responding to tim riffe and telling him that his function threw an error with the problem posed. He may have used your redefinition of the problem but he didn't say so.

                      – 42-
                      Sep 14 '12 at 20:14














                      yeah, I also did, stringsAsFactors=TRUE on my comp, but didn't mention this

                      – tim riffe
                      Sep 14 '12 at 20:31





                      yeah, I also did, stringsAsFactors=TRUE on my comp, but didn't mention this

                      – tim riffe
                      Sep 14 '12 at 20:31













                      Searching for a string in a factor will work i.e. str_count(d$factor_column,'A') but not vice-versa

                      – Nitro
                      Sep 27 '17 at 19:24






                      Searching for a string in a factor will work i.e. str_count(d$factor_column,'A') but not vice-versa

                      – Nitro
                      Sep 27 '17 at 19:24














                      48














                      If you don't want to leave base R, here's a fairly succinct and expressive possibility:



                      x <- q.data$string
                      lengths(regmatches(x, gregexpr("a", x)))
                      # [1] 2 1 0





                      share|improve this answer




















                      • 2





                        OK -- maybe that will only feel expressive once you've used the regmatches and gregexpr together a few times, but that combo is powerful enough that I thought it deserved a plug.

                        – Josh O'Brien
                        Sep 14 '12 at 15:48











                      • +1 for regmatches, I was not aware of the function.

                        – Roman Luštrik
                        Sep 14 '12 at 16:06











                      • regmatches is relatively new. It was introduced in 2.14.

                        – Dason
                        Sep 15 '12 at 17:49











                      • I don't think you need the regmatches bit. The function gregexpr returns a list with the indices of the matched occurrences for each element of x.

                        – savagent
                        Aug 26 '14 at 3:27






                      • 1





                        Sorry, I forgot about the -1. It only works if each line has at least one match, sapply(gregexpr("g", q.data$string), length).

                        – savagent
                        Aug 26 '14 at 4:42















                      48














                      If you don't want to leave base R, here's a fairly succinct and expressive possibility:



                      x <- q.data$string
                      lengths(regmatches(x, gregexpr("a", x)))
                      # [1] 2 1 0





                      share|improve this answer




















                      • 2





                        OK -- maybe that will only feel expressive once you've used the regmatches and gregexpr together a few times, but that combo is powerful enough that I thought it deserved a plug.

                        – Josh O'Brien
                        Sep 14 '12 at 15:48











                      • +1 for regmatches, I was not aware of the function.

                        – Roman Luštrik
                        Sep 14 '12 at 16:06











                      • regmatches is relatively new. It was introduced in 2.14.

                        – Dason
                        Sep 15 '12 at 17:49











                      • I don't think you need the regmatches bit. The function gregexpr returns a list with the indices of the matched occurrences for each element of x.

                        – savagent
                        Aug 26 '14 at 3:27






                      • 1





                        Sorry, I forgot about the -1. It only works if each line has at least one match, sapply(gregexpr("g", q.data$string), length).

                        – savagent
                        Aug 26 '14 at 4:42













                      48












                      48








                      48







                      If you don't want to leave base R, here's a fairly succinct and expressive possibility:



                      x <- q.data$string
                      lengths(regmatches(x, gregexpr("a", x)))
                      # [1] 2 1 0





                      share|improve this answer















                      If you don't want to leave base R, here's a fairly succinct and expressive possibility:



                      x <- q.data$string
                      lengths(regmatches(x, gregexpr("a", x)))
                      # [1] 2 1 0






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 27 at 11:55









                      zx8754

                      30.3k764101




                      30.3k764101










                      answered Sep 14 '12 at 15:44









                      Josh O'BrienJosh O'Brien

                      129k18279388




                      129k18279388







                      • 2





                        OK -- maybe that will only feel expressive once you've used the regmatches and gregexpr together a few times, but that combo is powerful enough that I thought it deserved a plug.

                        – Josh O'Brien
                        Sep 14 '12 at 15:48











                      • +1 for regmatches, I was not aware of the function.

                        – Roman Luštrik
                        Sep 14 '12 at 16:06











                      • regmatches is relatively new. It was introduced in 2.14.

                        – Dason
                        Sep 15 '12 at 17:49











                      • I don't think you need the regmatches bit. The function gregexpr returns a list with the indices of the matched occurrences for each element of x.

                        – savagent
                        Aug 26 '14 at 3:27






                      • 1





                        Sorry, I forgot about the -1. It only works if each line has at least one match, sapply(gregexpr("g", q.data$string), length).

                        – savagent
                        Aug 26 '14 at 4:42












                      • 2





                        OK -- maybe that will only feel expressive once you've used the regmatches and gregexpr together a few times, but that combo is powerful enough that I thought it deserved a plug.

                        – Josh O'Brien
                        Sep 14 '12 at 15:48











                      • +1 for regmatches, I was not aware of the function.

                        – Roman Luštrik
                        Sep 14 '12 at 16:06











                      • regmatches is relatively new. It was introduced in 2.14.

                        – Dason
                        Sep 15 '12 at 17:49











                      • I don't think you need the regmatches bit. The function gregexpr returns a list with the indices of the matched occurrences for each element of x.

                        – savagent
                        Aug 26 '14 at 3:27






                      • 1





                        Sorry, I forgot about the -1. It only works if each line has at least one match, sapply(gregexpr("g", q.data$string), length).

                        – savagent
                        Aug 26 '14 at 4:42







                      2




                      2





                      OK -- maybe that will only feel expressive once you've used the regmatches and gregexpr together a few times, but that combo is powerful enough that I thought it deserved a plug.

                      – Josh O'Brien
                      Sep 14 '12 at 15:48





                      OK -- maybe that will only feel expressive once you've used the regmatches and gregexpr together a few times, but that combo is powerful enough that I thought it deserved a plug.

                      – Josh O'Brien
                      Sep 14 '12 at 15:48













                      +1 for regmatches, I was not aware of the function.

                      – Roman Luštrik
                      Sep 14 '12 at 16:06





                      +1 for regmatches, I was not aware of the function.

                      – Roman Luštrik
                      Sep 14 '12 at 16:06













                      regmatches is relatively new. It was introduced in 2.14.

                      – Dason
                      Sep 15 '12 at 17:49





                      regmatches is relatively new. It was introduced in 2.14.

                      – Dason
                      Sep 15 '12 at 17:49













                      I don't think you need the regmatches bit. The function gregexpr returns a list with the indices of the matched occurrences for each element of x.

                      – savagent
                      Aug 26 '14 at 3:27





                      I don't think you need the regmatches bit. The function gregexpr returns a list with the indices of the matched occurrences for each element of x.

                      – savagent
                      Aug 26 '14 at 3:27




                      1




                      1





                      Sorry, I forgot about the -1. It only works if each line has at least one match, sapply(gregexpr("g", q.data$string), length).

                      – savagent
                      Aug 26 '14 at 4:42





                      Sorry, I forgot about the -1. It only works if each line has at least one match, sapply(gregexpr("g", q.data$string), length).

                      – savagent
                      Aug 26 '14 at 4:42











                      12














                      nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string))
                      [1] 2 1 0


                      Notice that I coerce the factor variable to character, before passing to nchar. The regex functions appear to do that internally.



                      Here's benchmark results (with a scaled up size of the test to 3000 rows)



                       q.data<-q.data[rep(1:NROW(q.data), 1000),]
                      str(q.data)
                      'data.frame': 3000 obs. of 3 variables:
                      $ number : int 1 2 3 1 2 3 1 2 3 1 ...
                      $ string : Factor w/ 3 levels "greatgreat","magic",..: 1 2 3 1 2 3 1 2 3 1 ...
                      $ number.of.a: int 2 1 0 2 1 0 2 1 0 2 ...

                      benchmark( Dason = q.data$number.of.a <- str_count(as.character(q.data$string), "a") ,
                      Tim = resT <- sapply(as.character(q.data$string), function(x, letter = "a")
                      sum(unlist(strsplit(x, split = "")) == letter) ) ,

                      DWin = resW <- nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string)),
                      Josh = x <- sapply(regmatches(q.data$string, gregexpr("g",q.data$string )), length), replications=100)
                      #-----------------------
                      test replications elapsed relative user.self sys.self user.child sys.child
                      1 Dason 100 4.173 9.959427 2.985 1.204 0 0
                      3 DWin 100 0.419 1.000000 0.417 0.003 0 0
                      4 Josh 100 18.635 44.474940 17.883 0.827 0 0
                      2 Tim 100 3.705 8.842482 3.646 0.072 0 0





                      share|improve this answer




















                      • 1





                        This is the fastest solution in the answers but is made ~30% faster on your benchmark by passing the optional fixed=TRUE to gsub. There are also cases where fixed=TRUE would be required (i.e., when the character you want to count could be interpreted as a regex assertion such as .).

                        – C8H10N4O2
                        Jan 16 '18 at 16:15
















                      12














                      nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string))
                      [1] 2 1 0


                      Notice that I coerce the factor variable to character, before passing to nchar. The regex functions appear to do that internally.



                      Here's benchmark results (with a scaled up size of the test to 3000 rows)



                       q.data<-q.data[rep(1:NROW(q.data), 1000),]
                      str(q.data)
                      'data.frame': 3000 obs. of 3 variables:
                      $ number : int 1 2 3 1 2 3 1 2 3 1 ...
                      $ string : Factor w/ 3 levels "greatgreat","magic",..: 1 2 3 1 2 3 1 2 3 1 ...
                      $ number.of.a: int 2 1 0 2 1 0 2 1 0 2 ...

                      benchmark( Dason = q.data$number.of.a <- str_count(as.character(q.data$string), "a") ,
                      Tim = resT <- sapply(as.character(q.data$string), function(x, letter = "a")
                      sum(unlist(strsplit(x, split = "")) == letter) ) ,

                      DWin = resW <- nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string)),
                      Josh = x <- sapply(regmatches(q.data$string, gregexpr("g",q.data$string )), length), replications=100)
                      #-----------------------
                      test replications elapsed relative user.self sys.self user.child sys.child
                      1 Dason 100 4.173 9.959427 2.985 1.204 0 0
                      3 DWin 100 0.419 1.000000 0.417 0.003 0 0
                      4 Josh 100 18.635 44.474940 17.883 0.827 0 0
                      2 Tim 100 3.705 8.842482 3.646 0.072 0 0





                      share|improve this answer




















                      • 1





                        This is the fastest solution in the answers but is made ~30% faster on your benchmark by passing the optional fixed=TRUE to gsub. There are also cases where fixed=TRUE would be required (i.e., when the character you want to count could be interpreted as a regex assertion such as .).

                        – C8H10N4O2
                        Jan 16 '18 at 16:15














                      12












                      12








                      12







                      nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string))
                      [1] 2 1 0


                      Notice that I coerce the factor variable to character, before passing to nchar. The regex functions appear to do that internally.



                      Here's benchmark results (with a scaled up size of the test to 3000 rows)



                       q.data<-q.data[rep(1:NROW(q.data), 1000),]
                      str(q.data)
                      'data.frame': 3000 obs. of 3 variables:
                      $ number : int 1 2 3 1 2 3 1 2 3 1 ...
                      $ string : Factor w/ 3 levels "greatgreat","magic",..: 1 2 3 1 2 3 1 2 3 1 ...
                      $ number.of.a: int 2 1 0 2 1 0 2 1 0 2 ...

                      benchmark( Dason = q.data$number.of.a <- str_count(as.character(q.data$string), "a") ,
                      Tim = resT <- sapply(as.character(q.data$string), function(x, letter = "a")
                      sum(unlist(strsplit(x, split = "")) == letter) ) ,

                      DWin = resW <- nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string)),
                      Josh = x <- sapply(regmatches(q.data$string, gregexpr("g",q.data$string )), length), replications=100)
                      #-----------------------
                      test replications elapsed relative user.self sys.self user.child sys.child
                      1 Dason 100 4.173 9.959427 2.985 1.204 0 0
                      3 DWin 100 0.419 1.000000 0.417 0.003 0 0
                      4 Josh 100 18.635 44.474940 17.883 0.827 0 0
                      2 Tim 100 3.705 8.842482 3.646 0.072 0 0





                      share|improve this answer















                      nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string))
                      [1] 2 1 0


                      Notice that I coerce the factor variable to character, before passing to nchar. The regex functions appear to do that internally.



                      Here's benchmark results (with a scaled up size of the test to 3000 rows)



                       q.data<-q.data[rep(1:NROW(q.data), 1000),]
                      str(q.data)
                      'data.frame': 3000 obs. of 3 variables:
                      $ number : int 1 2 3 1 2 3 1 2 3 1 ...
                      $ string : Factor w/ 3 levels "greatgreat","magic",..: 1 2 3 1 2 3 1 2 3 1 ...
                      $ number.of.a: int 2 1 0 2 1 0 2 1 0 2 ...

                      benchmark( Dason = q.data$number.of.a <- str_count(as.character(q.data$string), "a") ,
                      Tim = resT <- sapply(as.character(q.data$string), function(x, letter = "a")
                      sum(unlist(strsplit(x, split = "")) == letter) ) ,

                      DWin = resW <- nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string)),
                      Josh = x <- sapply(regmatches(q.data$string, gregexpr("g",q.data$string )), length), replications=100)
                      #-----------------------
                      test replications elapsed relative user.self sys.self user.child sys.child
                      1 Dason 100 4.173 9.959427 2.985 1.204 0 0
                      3 DWin 100 0.419 1.000000 0.417 0.003 0 0
                      4 Josh 100 18.635 44.474940 17.883 0.827 0 0
                      2 Tim 100 3.705 8.842482 3.646 0.072 0 0






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Sep 14 '12 at 20:41

























                      answered Sep 14 '12 at 19:23









                      42-42-

                      215k15263401




                      215k15263401







                      • 1





                        This is the fastest solution in the answers but is made ~30% faster on your benchmark by passing the optional fixed=TRUE to gsub. There are also cases where fixed=TRUE would be required (i.e., when the character you want to count could be interpreted as a regex assertion such as .).

                        – C8H10N4O2
                        Jan 16 '18 at 16:15













                      • 1





                        This is the fastest solution in the answers but is made ~30% faster on your benchmark by passing the optional fixed=TRUE to gsub. There are also cases where fixed=TRUE would be required (i.e., when the character you want to count could be interpreted as a regex assertion such as .).

                        – C8H10N4O2
                        Jan 16 '18 at 16:15








                      1




                      1





                      This is the fastest solution in the answers but is made ~30% faster on your benchmark by passing the optional fixed=TRUE to gsub. There are also cases where fixed=TRUE would be required (i.e., when the character you want to count could be interpreted as a regex assertion such as .).

                      – C8H10N4O2
                      Jan 16 '18 at 16:15






                      This is the fastest solution in the answers but is made ~30% faster on your benchmark by passing the optional fixed=TRUE to gsub. There are also cases where fixed=TRUE would be required (i.e., when the character you want to count could be interpreted as a regex assertion such as .).

                      – C8H10N4O2
                      Jan 16 '18 at 16:15












                      5














                      sum(charToRaw("abc.d.aa") == charToRaw('.'))


                      is an good option.






                      share|improve this answer





























                        5














                        sum(charToRaw("abc.d.aa") == charToRaw('.'))


                        is an good option.






                        share|improve this answer



























                          5












                          5








                          5







                          sum(charToRaw("abc.d.aa") == charToRaw('.'))


                          is an good option.






                          share|improve this answer















                          sum(charToRaw("abc.d.aa") == charToRaw('.'))


                          is an good option.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 6 '16 at 16:24









                          Andy

                          31.3k21105163




                          31.3k21105163










                          answered Jul 6 '16 at 16:17









                          Zhang TaoZhang Tao

                          5111




                          5111





















                              2














                              I'm sure someone can do better, but this works:



                              sapply(as.character(q.data$string), function(x, letter = "a")
                              sum(unlist(strsplit(x, split = "")) == letter)
                              )
                              greatgreat magic not
                              2 1 0


                              or in a function:



                              countLetter <- function(charvec, letter)
                              sapply(charvec, function(x, letter)
                              sum(unlist(strsplit(x, split = "")) == letter)
                              , letter = letter)

                              countLetter(as.character(q.data$string),"a")





                              share|improve this answer

























                              • I seem to get an error with the first one ... and the second one... (was trying to benchmark all of these.)

                                – 42-
                                Sep 14 '12 at 20:04












                              • Use as.character()

                                – 42-
                                Sep 14 '12 at 20:10











                              • edited to reflect this, thx

                                – tim riffe
                                Sep 14 '12 at 20:31















                              2














                              I'm sure someone can do better, but this works:



                              sapply(as.character(q.data$string), function(x, letter = "a")
                              sum(unlist(strsplit(x, split = "")) == letter)
                              )
                              greatgreat magic not
                              2 1 0


                              or in a function:



                              countLetter <- function(charvec, letter)
                              sapply(charvec, function(x, letter)
                              sum(unlist(strsplit(x, split = "")) == letter)
                              , letter = letter)

                              countLetter(as.character(q.data$string),"a")





                              share|improve this answer

























                              • I seem to get an error with the first one ... and the second one... (was trying to benchmark all of these.)

                                – 42-
                                Sep 14 '12 at 20:04












                              • Use as.character()

                                – 42-
                                Sep 14 '12 at 20:10











                              • edited to reflect this, thx

                                – tim riffe
                                Sep 14 '12 at 20:31













                              2












                              2








                              2







                              I'm sure someone can do better, but this works:



                              sapply(as.character(q.data$string), function(x, letter = "a")
                              sum(unlist(strsplit(x, split = "")) == letter)
                              )
                              greatgreat magic not
                              2 1 0


                              or in a function:



                              countLetter <- function(charvec, letter)
                              sapply(charvec, function(x, letter)
                              sum(unlist(strsplit(x, split = "")) == letter)
                              , letter = letter)

                              countLetter(as.character(q.data$string),"a")





                              share|improve this answer















                              I'm sure someone can do better, but this works:



                              sapply(as.character(q.data$string), function(x, letter = "a")
                              sum(unlist(strsplit(x, split = "")) == letter)
                              )
                              greatgreat magic not
                              2 1 0


                              or in a function:



                              countLetter <- function(charvec, letter)
                              sapply(charvec, function(x, letter)
                              sum(unlist(strsplit(x, split = "")) == letter)
                              , letter = letter)

                              countLetter(as.character(q.data$string),"a")






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Sep 14 '12 at 20:31

























                              answered Sep 14 '12 at 15:23









                              tim riffetim riffe

                              4,6881834




                              4,6881834












                              • I seem to get an error with the first one ... and the second one... (was trying to benchmark all of these.)

                                – 42-
                                Sep 14 '12 at 20:04












                              • Use as.character()

                                – 42-
                                Sep 14 '12 at 20:10











                              • edited to reflect this, thx

                                – tim riffe
                                Sep 14 '12 at 20:31

















                              • I seem to get an error with the first one ... and the second one... (was trying to benchmark all of these.)

                                – 42-
                                Sep 14 '12 at 20:04












                              • Use as.character()

                                – 42-
                                Sep 14 '12 at 20:10











                              • edited to reflect this, thx

                                – tim riffe
                                Sep 14 '12 at 20:31
















                              I seem to get an error with the first one ... and the second one... (was trying to benchmark all of these.)

                              – 42-
                              Sep 14 '12 at 20:04






                              I seem to get an error with the first one ... and the second one... (was trying to benchmark all of these.)

                              – 42-
                              Sep 14 '12 at 20:04














                              Use as.character()

                              – 42-
                              Sep 14 '12 at 20:10





                              Use as.character()

                              – 42-
                              Sep 14 '12 at 20:10













                              edited to reflect this, thx

                              – tim riffe
                              Sep 14 '12 at 20:31





                              edited to reflect this, thx

                              – tim riffe
                              Sep 14 '12 at 20:31











                              0














                              I count characters same way as Amarjeet. However I prefer to do it in a single line.



                              HowManySpaces<-nchar(DF$string)-nchar(gsub(" ","",DF$string)) # count spaces in DF$string





                              share|improve this answer





























                                0














                                I count characters same way as Amarjeet. However I prefer to do it in a single line.



                                HowManySpaces<-nchar(DF$string)-nchar(gsub(" ","",DF$string)) # count spaces in DF$string





                                share|improve this answer



























                                  0












                                  0








                                  0







                                  I count characters same way as Amarjeet. However I prefer to do it in a single line.



                                  HowManySpaces<-nchar(DF$string)-nchar(gsub(" ","",DF$string)) # count spaces in DF$string





                                  share|improve this answer















                                  I count characters same way as Amarjeet. However I prefer to do it in a single line.



                                  HowManySpaces<-nchar(DF$string)-nchar(gsub(" ","",DF$string)) # count spaces in DF$string






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 13 '17 at 12:29

























                                  answered Nov 13 '17 at 12:09









                                  cineS.cineS.

                                  62




                                  62





















                                      0














                                      The easiest and the cleanest way IMHO is :



                                      q.data$number.of.a <- lengths(gregexpr('a', q.data$string))

                                      # number string number.of.a`
                                      #1 1 greatgreat 2`
                                      #2 2 magic 1`
                                      #3 3 not 0`





                                      share|improve this answer



























                                        0














                                        The easiest and the cleanest way IMHO is :



                                        q.data$number.of.a <- lengths(gregexpr('a', q.data$string))

                                        # number string number.of.a`
                                        #1 1 greatgreat 2`
                                        #2 2 magic 1`
                                        #3 3 not 0`





                                        share|improve this answer

























                                          0












                                          0








                                          0







                                          The easiest and the cleanest way IMHO is :



                                          q.data$number.of.a <- lengths(gregexpr('a', q.data$string))

                                          # number string number.of.a`
                                          #1 1 greatgreat 2`
                                          #2 2 magic 1`
                                          #3 3 not 0`





                                          share|improve this answer













                                          The easiest and the cleanest way IMHO is :



                                          q.data$number.of.a <- lengths(gregexpr('a', q.data$string))

                                          # number string number.of.a`
                                          #1 1 greatgreat 2`
                                          #2 2 magic 1`
                                          #3 3 not 0`






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Dec 26 '17 at 9:54









                                          Giovanni CampagnoliGiovanni Campagnoli

                                          264




                                          264





















                                              0














                                              You could just use string division



                                              require(roperators)
                                              my_strings <- c('apple', banana', 'pear', 'melon')
                                              my_strings %s/% 'a'


                                              Which will give you 1, 3, 1, 0. You can also use string division with regular expressions and whole words.






                                              share|improve this answer



























                                                0














                                                You could just use string division



                                                require(roperators)
                                                my_strings <- c('apple', banana', 'pear', 'melon')
                                                my_strings %s/% 'a'


                                                Which will give you 1, 3, 1, 0. You can also use string division with regular expressions and whole words.






                                                share|improve this answer

























                                                  0












                                                  0








                                                  0







                                                  You could just use string division



                                                  require(roperators)
                                                  my_strings <- c('apple', banana', 'pear', 'melon')
                                                  my_strings %s/% 'a'


                                                  Which will give you 1, 3, 1, 0. You can also use string division with regular expressions and whole words.






                                                  share|improve this answer













                                                  You could just use string division



                                                  require(roperators)
                                                  my_strings <- c('apple', banana', 'pear', 'melon')
                                                  my_strings %s/% 'a'


                                                  Which will give you 1, 3, 1, 0. You can also use string division with regular expressions and whole words.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Oct 3 '18 at 16:15









                                                  BenbobBenbob

                                                  312




                                                  312





















                                                      0














                                                      The stringi package provides the functions stri_count and stri_count_fixed which are very fast.



                                                      stringi::stri_count(q.data$string, fixed = "a")
                                                      # [1] 2 1 0


                                                      benchmark



                                                      Compared to the fastest approach from @42-'s answer and to the equivalent function from the stringr package for a vector with 30.000 elements.



                                                      library(microbenchmark)

                                                      benchmark <- microbenchmark(
                                                      stringi = stringi::stri_count(test.data$string, fixed = "a"),
                                                      baseR = nchar(test.data$string) - nchar(gsub("a", "", test.data$string, fixed = TRUE)),
                                                      stringr = str_count(test.data$string, "a")
                                                      )

                                                      autoplot(benchmark)


                                                      data



                                                      q.data <- data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = FALSE)
                                                      test.data <- q.data[rep(1:NROW(q.data), 10000),]


                                                      enter image description here






                                                      share|improve this answer





























                                                        0














                                                        The stringi package provides the functions stri_count and stri_count_fixed which are very fast.



                                                        stringi::stri_count(q.data$string, fixed = "a")
                                                        # [1] 2 1 0


                                                        benchmark



                                                        Compared to the fastest approach from @42-'s answer and to the equivalent function from the stringr package for a vector with 30.000 elements.



                                                        library(microbenchmark)

                                                        benchmark <- microbenchmark(
                                                        stringi = stringi::stri_count(test.data$string, fixed = "a"),
                                                        baseR = nchar(test.data$string) - nchar(gsub("a", "", test.data$string, fixed = TRUE)),
                                                        stringr = str_count(test.data$string, "a")
                                                        )

                                                        autoplot(benchmark)


                                                        data



                                                        q.data <- data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = FALSE)
                                                        test.data <- q.data[rep(1:NROW(q.data), 10000),]


                                                        enter image description here






                                                        share|improve this answer



























                                                          0












                                                          0








                                                          0







                                                          The stringi package provides the functions stri_count and stri_count_fixed which are very fast.



                                                          stringi::stri_count(q.data$string, fixed = "a")
                                                          # [1] 2 1 0


                                                          benchmark



                                                          Compared to the fastest approach from @42-'s answer and to the equivalent function from the stringr package for a vector with 30.000 elements.



                                                          library(microbenchmark)

                                                          benchmark <- microbenchmark(
                                                          stringi = stringi::stri_count(test.data$string, fixed = "a"),
                                                          baseR = nchar(test.data$string) - nchar(gsub("a", "", test.data$string, fixed = TRUE)),
                                                          stringr = str_count(test.data$string, "a")
                                                          )

                                                          autoplot(benchmark)


                                                          data



                                                          q.data <- data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = FALSE)
                                                          test.data <- q.data[rep(1:NROW(q.data), 10000),]


                                                          enter image description here






                                                          share|improve this answer















                                                          The stringi package provides the functions stri_count and stri_count_fixed which are very fast.



                                                          stringi::stri_count(q.data$string, fixed = "a")
                                                          # [1] 2 1 0


                                                          benchmark



                                                          Compared to the fastest approach from @42-'s answer and to the equivalent function from the stringr package for a vector with 30.000 elements.



                                                          library(microbenchmark)

                                                          benchmark <- microbenchmark(
                                                          stringi = stringi::stri_count(test.data$string, fixed = "a"),
                                                          baseR = nchar(test.data$string) - nchar(gsub("a", "", test.data$string, fixed = TRUE)),
                                                          stringr = str_count(test.data$string, "a")
                                                          )

                                                          autoplot(benchmark)


                                                          data



                                                          q.data <- data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = FALSE)
                                                          test.data <- q.data[rep(1:NROW(q.data), 10000),]


                                                          enter image description here







                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Mar 7 at 20:35

























                                                          answered Mar 7 at 20:27









                                                          markusmarkus

                                                          14.5k11336




                                                          14.5k11336





















                                                              0














                                                              The question below has been moved here, but it seems this page doesn't directly answer to Farah El's question.
                                                              How to find number 1s in 101 in R



                                                              So, I'll write an answer here, just in case.



                                                              library(magrittr)
                                                              n %>% # n is a number you'd like to inspect
                                                              as.character() %>%
                                                              str_count(pattern = "1")


                                                              https://stackoverflow.com/users/8931457/farah-el






                                                              share|improve this answer



























                                                                0














                                                                The question below has been moved here, but it seems this page doesn't directly answer to Farah El's question.
                                                                How to find number 1s in 101 in R



                                                                So, I'll write an answer here, just in case.



                                                                library(magrittr)
                                                                n %>% # n is a number you'd like to inspect
                                                                as.character() %>%
                                                                str_count(pattern = "1")


                                                                https://stackoverflow.com/users/8931457/farah-el






                                                                share|improve this answer

























                                                                  0












                                                                  0








                                                                  0







                                                                  The question below has been moved here, but it seems this page doesn't directly answer to Farah El's question.
                                                                  How to find number 1s in 101 in R



                                                                  So, I'll write an answer here, just in case.



                                                                  library(magrittr)
                                                                  n %>% # n is a number you'd like to inspect
                                                                  as.character() %>%
                                                                  str_count(pattern = "1")


                                                                  https://stackoverflow.com/users/8931457/farah-el






                                                                  share|improve this answer













                                                                  The question below has been moved here, but it seems this page doesn't directly answer to Farah El's question.
                                                                  How to find number 1s in 101 in R



                                                                  So, I'll write an answer here, just in case.



                                                                  library(magrittr)
                                                                  n %>% # n is a number you'd like to inspect
                                                                  as.character() %>%
                                                                  str_count(pattern = "1")


                                                                  https://stackoverflow.com/users/8931457/farah-el







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered yesterday









                                                                  YoshiakiYoshiaki

                                                                  13




                                                                  13





















                                                                      -1














                                                                      s <- "aababacababaaathhhhhslsls jsjsjjsaa ghhaalll"
                                                                      p <- "a"
                                                                      s2 <- gsub(p,"",s)
                                                                      numOcc <- nchar(s) - nchar(s2)


                                                                      May not be the efficient one but solve my purpose.






                                                                      share|improve this answer


















                                                                      • 2





                                                                        This is exactly same as this answer.

                                                                        – zx8754
                                                                        Apr 6 '16 at 11:29















                                                                      -1














                                                                      s <- "aababacababaaathhhhhslsls jsjsjjsaa ghhaalll"
                                                                      p <- "a"
                                                                      s2 <- gsub(p,"",s)
                                                                      numOcc <- nchar(s) - nchar(s2)


                                                                      May not be the efficient one but solve my purpose.






                                                                      share|improve this answer


















                                                                      • 2





                                                                        This is exactly same as this answer.

                                                                        – zx8754
                                                                        Apr 6 '16 at 11:29













                                                                      -1












                                                                      -1








                                                                      -1







                                                                      s <- "aababacababaaathhhhhslsls jsjsjjsaa ghhaalll"
                                                                      p <- "a"
                                                                      s2 <- gsub(p,"",s)
                                                                      numOcc <- nchar(s) - nchar(s2)


                                                                      May not be the efficient one but solve my purpose.






                                                                      share|improve this answer













                                                                      s <- "aababacababaaathhhhhslsls jsjsjjsaa ghhaalll"
                                                                      p <- "a"
                                                                      s2 <- gsub(p,"",s)
                                                                      numOcc <- nchar(s) - nchar(s2)


                                                                      May not be the efficient one but solve my purpose.







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered May 8 '15 at 6:00









                                                                      AmarjeetAmarjeet

                                                                      4761513




                                                                      4761513







                                                                      • 2





                                                                        This is exactly same as this answer.

                                                                        – zx8754
                                                                        Apr 6 '16 at 11:29












                                                                      • 2





                                                                        This is exactly same as this answer.

                                                                        – zx8754
                                                                        Apr 6 '16 at 11:29







                                                                      2




                                                                      2





                                                                      This is exactly same as this answer.

                                                                      – zx8754
                                                                      Apr 6 '16 at 11:29





                                                                      This is exactly same as this answer.

                                                                      – zx8754
                                                                      Apr 6 '16 at 11:29

















                                                                      draft saved

                                                                      draft discarded
















































                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid


                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.

                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function ()
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12427385%2fhow-to-calculate-the-number-of-occurrence-of-a-given-character-in-each-row-of-a%23new-answer', 'question_page');

                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                                                                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                                                                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived