How to count text but excluding the words in brackets?












-4















I want to generate a new column shows counts of words for each film in the data frame, but some films' name including a bracket, and I don't want to count the words within the bracket, how can I code it? By the way, I am trying to use str.count function and I show my code that counts all words.



movie <- movie %>% mutate(words.of.title = str_count(str_trim(film), "\S+"))


Example dataframe










share|improve this question





























    -4















    I want to generate a new column shows counts of words for each film in the data frame, but some films' name including a bracket, and I don't want to count the words within the bracket, how can I code it? By the way, I am trying to use str.count function and I show my code that counts all words.



    movie <- movie %>% mutate(words.of.title = str_count(str_trim(film), "\S+"))


    Example dataframe










    share|improve this question



























      -4












      -4








      -4








      I want to generate a new column shows counts of words for each film in the data frame, but some films' name including a bracket, and I don't want to count the words within the bracket, how can I code it? By the way, I am trying to use str.count function and I show my code that counts all words.



      movie <- movie %>% mutate(words.of.title = str_count(str_trim(film), "\S+"))


      Example dataframe










      share|improve this question
















      I want to generate a new column shows counts of words for each film in the data frame, but some films' name including a bracket, and I don't want to count the words within the bracket, how can I code it? By the way, I am trying to use str.count function and I show my code that counts all words.



      movie <- movie %>% mutate(words.of.title = str_count(str_trim(film), "\S+"))


      Example dataframe







      r rstudio






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 8:35









      Emily Kothe

      422114




      422114










      asked Jan 1 at 23:08









      honghong

      1




      1
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Here is an option to check the count of words (alphanumeric characters) that are separated with either a space (\s) or at the start of the string (^)



          library(tidyverse)
          movie %>%
          mutate(words.of.title=str_count(str_trim(film), "(^|\s)[[:alnum:]]+"))





          share|improve this answer
























          • Thank you, it is a simple code. but it doesn't work very well if there are more than one word in the bracket, such as "Blade Runner: The Final Cut (Secret Cinema 2018)", and the result is 7, but it should be 5. Can it be improved? Thank you !!

            – hong
            Jan 2 at 10:13



















          0














          You can do this by using gsub to remove the brackets and text within them. See below for a reprex and solution.



          library(dplyr)
          library(stringr)

          df <- data.frame(id=c(1,2,3),
          film=c('Film','Film with (brackets)','Another film'))

          movie <- df %>%
          mutate(words.of.title = str_count(str_trim(gsub('\(.*?\)', '', film)), "\S+"))

          movie
          #> id film words.of.title
          #> 1 1 Film 1
          #> 2 2 Film with (brackets) 2
          #> 3 3 Another film 2


          Created on 2019-01-02 by the reprex package (v0.2.0).






          share|improve this answer
























          • Thank you very much, it works.

            – hong
            Jan 2 at 9:58











          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%2f53999634%2fhow-to-count-text-but-excluding-the-words-in-brackets%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Here is an option to check the count of words (alphanumeric characters) that are separated with either a space (\s) or at the start of the string (^)



          library(tidyverse)
          movie %>%
          mutate(words.of.title=str_count(str_trim(film), "(^|\s)[[:alnum:]]+"))





          share|improve this answer
























          • Thank you, it is a simple code. but it doesn't work very well if there are more than one word in the bracket, such as "Blade Runner: The Final Cut (Secret Cinema 2018)", and the result is 7, but it should be 5. Can it be improved? Thank you !!

            – hong
            Jan 2 at 10:13
















          1














          Here is an option to check the count of words (alphanumeric characters) that are separated with either a space (\s) or at the start of the string (^)



          library(tidyverse)
          movie %>%
          mutate(words.of.title=str_count(str_trim(film), "(^|\s)[[:alnum:]]+"))





          share|improve this answer
























          • Thank you, it is a simple code. but it doesn't work very well if there are more than one word in the bracket, such as "Blade Runner: The Final Cut (Secret Cinema 2018)", and the result is 7, but it should be 5. Can it be improved? Thank you !!

            – hong
            Jan 2 at 10:13














          1












          1








          1







          Here is an option to check the count of words (alphanumeric characters) that are separated with either a space (\s) or at the start of the string (^)



          library(tidyverse)
          movie %>%
          mutate(words.of.title=str_count(str_trim(film), "(^|\s)[[:alnum:]]+"))





          share|improve this answer













          Here is an option to check the count of words (alphanumeric characters) that are separated with either a space (\s) or at the start of the string (^)



          library(tidyverse)
          movie %>%
          mutate(words.of.title=str_count(str_trim(film), "(^|\s)[[:alnum:]]+"))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 23:48









          akrunakrun

          411k13199274




          411k13199274













          • Thank you, it is a simple code. but it doesn't work very well if there are more than one word in the bracket, such as "Blade Runner: The Final Cut (Secret Cinema 2018)", and the result is 7, but it should be 5. Can it be improved? Thank you !!

            – hong
            Jan 2 at 10:13



















          • Thank you, it is a simple code. but it doesn't work very well if there are more than one word in the bracket, such as "Blade Runner: The Final Cut (Secret Cinema 2018)", and the result is 7, but it should be 5. Can it be improved? Thank you !!

            – hong
            Jan 2 at 10:13

















          Thank you, it is a simple code. but it doesn't work very well if there are more than one word in the bracket, such as "Blade Runner: The Final Cut (Secret Cinema 2018)", and the result is 7, but it should be 5. Can it be improved? Thank you !!

          – hong
          Jan 2 at 10:13





          Thank you, it is a simple code. but it doesn't work very well if there are more than one word in the bracket, such as "Blade Runner: The Final Cut (Secret Cinema 2018)", and the result is 7, but it should be 5. Can it be improved? Thank you !!

          – hong
          Jan 2 at 10:13













          0














          You can do this by using gsub to remove the brackets and text within them. See below for a reprex and solution.



          library(dplyr)
          library(stringr)

          df <- data.frame(id=c(1,2,3),
          film=c('Film','Film with (brackets)','Another film'))

          movie <- df %>%
          mutate(words.of.title = str_count(str_trim(gsub('\(.*?\)', '', film)), "\S+"))

          movie
          #> id film words.of.title
          #> 1 1 Film 1
          #> 2 2 Film with (brackets) 2
          #> 3 3 Another film 2


          Created on 2019-01-02 by the reprex package (v0.2.0).






          share|improve this answer
























          • Thank you very much, it works.

            – hong
            Jan 2 at 9:58
















          0














          You can do this by using gsub to remove the brackets and text within them. See below for a reprex and solution.



          library(dplyr)
          library(stringr)

          df <- data.frame(id=c(1,2,3),
          film=c('Film','Film with (brackets)','Another film'))

          movie <- df %>%
          mutate(words.of.title = str_count(str_trim(gsub('\(.*?\)', '', film)), "\S+"))

          movie
          #> id film words.of.title
          #> 1 1 Film 1
          #> 2 2 Film with (brackets) 2
          #> 3 3 Another film 2


          Created on 2019-01-02 by the reprex package (v0.2.0).






          share|improve this answer
























          • Thank you very much, it works.

            – hong
            Jan 2 at 9:58














          0












          0








          0







          You can do this by using gsub to remove the brackets and text within them. See below for a reprex and solution.



          library(dplyr)
          library(stringr)

          df <- data.frame(id=c(1,2,3),
          film=c('Film','Film with (brackets)','Another film'))

          movie <- df %>%
          mutate(words.of.title = str_count(str_trim(gsub('\(.*?\)', '', film)), "\S+"))

          movie
          #> id film words.of.title
          #> 1 1 Film 1
          #> 2 2 Film with (brackets) 2
          #> 3 3 Another film 2


          Created on 2019-01-02 by the reprex package (v0.2.0).






          share|improve this answer













          You can do this by using gsub to remove the brackets and text within them. See below for a reprex and solution.



          library(dplyr)
          library(stringr)

          df <- data.frame(id=c(1,2,3),
          film=c('Film','Film with (brackets)','Another film'))

          movie <- df %>%
          mutate(words.of.title = str_count(str_trim(gsub('\(.*?\)', '', film)), "\S+"))

          movie
          #> id film words.of.title
          #> 1 1 Film 1
          #> 2 2 Film with (brackets) 2
          #> 3 3 Another film 2


          Created on 2019-01-02 by the reprex package (v0.2.0).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 23:33









          Emily KotheEmily Kothe

          422114




          422114













          • Thank you very much, it works.

            – hong
            Jan 2 at 9:58



















          • Thank you very much, it works.

            – hong
            Jan 2 at 9:58

















          Thank you very much, it works.

          – hong
          Jan 2 at 9:58





          Thank you very much, it works.

          – hong
          Jan 2 at 9:58


















          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%2f53999634%2fhow-to-count-text-but-excluding-the-words-in-brackets%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas