How to count text but excluding the words in brackets?
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+"))
r rstudio
add a comment |
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+"))
r rstudio
add a comment |
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+"))
r rstudio
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+"))
r rstudio
r rstudio
edited Jan 2 at 8:35
Emily Kothe
422114
422114
asked Jan 1 at 23:08
honghong
1
1
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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:]]+"))
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
add a comment |
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).
Thank you very much, it works.
– hong
Jan 2 at 9:58
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:]]+"))
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
add a comment |
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:]]+"))
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
add a comment |
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:]]+"))
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:]]+"))
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
add a comment |
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
add a comment |
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).
Thank you very much, it works.
– hong
Jan 2 at 9:58
add a comment |
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).
Thank you very much, it works.
– hong
Jan 2 at 9:58
add a comment |
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).
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).
answered Jan 1 at 23:33
Emily KotheEmily Kothe
422114
422114
Thank you very much, it works.
– hong
Jan 2 at 9:58
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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