Extract numerical value before a string in R
I have been mucking around with regex strings and strsplit but can't figure out how to solve my problem.
I have a collection of html documents that will always contain the phrase "people own these"
. I want to extract the number immediately preceding this phrase. i.e. '732,234 people own these' - I'm hoping to capture the number 732,234 (including the comma, though I don't care if it's removed).
The number and phrase are always surrounded by a . I tried using Xpath but that seemed even harder than a regex expression. Any help or advice is greatly appreciated!
example string: >742,811 people own these<
-> 742,811
r
add a comment |
I have been mucking around with regex strings and strsplit but can't figure out how to solve my problem.
I have a collection of html documents that will always contain the phrase "people own these"
. I want to extract the number immediately preceding this phrase. i.e. '732,234 people own these' - I'm hoping to capture the number 732,234 (including the comma, though I don't care if it's removed).
The number and phrase are always surrounded by a . I tried using Xpath but that seemed even harder than a regex expression. Any help or advice is greatly appreciated!
example string: >742,811 people own these<
-> 742,811
r
1
Please do not use regular expressions to work with HTML. Can you please post a representative sample of the actual HTML or a link to the source? You should be using XML operations. It's kind of sad two folks are aiding this path fraught with peril.
– hrbrmstr
Nov 14 '18 at 2:46
add a comment |
I have been mucking around with regex strings and strsplit but can't figure out how to solve my problem.
I have a collection of html documents that will always contain the phrase "people own these"
. I want to extract the number immediately preceding this phrase. i.e. '732,234 people own these' - I'm hoping to capture the number 732,234 (including the comma, though I don't care if it's removed).
The number and phrase are always surrounded by a . I tried using Xpath but that seemed even harder than a regex expression. Any help or advice is greatly appreciated!
example string: >742,811 people own these<
-> 742,811
r
I have been mucking around with regex strings and strsplit but can't figure out how to solve my problem.
I have a collection of html documents that will always contain the phrase "people own these"
. I want to extract the number immediately preceding this phrase. i.e. '732,234 people own these' - I'm hoping to capture the number 732,234 (including the comma, though I don't care if it's removed).
The number and phrase are always surrounded by a . I tried using Xpath but that seemed even harder than a regex expression. Any help or advice is greatly appreciated!
example string: >742,811 people own these<
-> 742,811
r
r
edited Nov 14 '18 at 2:45
hrbrmstr
60.8k688150
60.8k688150
asked Nov 14 '18 at 2:19
PermafrostPermafrost
275
275
1
Please do not use regular expressions to work with HTML. Can you please post a representative sample of the actual HTML or a link to the source? You should be using XML operations. It's kind of sad two folks are aiding this path fraught with peril.
– hrbrmstr
Nov 14 '18 at 2:46
add a comment |
1
Please do not use regular expressions to work with HTML. Can you please post a representative sample of the actual HTML or a link to the source? You should be using XML operations. It's kind of sad two folks are aiding this path fraught with peril.
– hrbrmstr
Nov 14 '18 at 2:46
1
1
Please do not use regular expressions to work with HTML. Can you please post a representative sample of the actual HTML or a link to the source? You should be using XML operations. It's kind of sad two folks are aiding this path fraught with peril.
– hrbrmstr
Nov 14 '18 at 2:46
Please do not use regular expressions to work with HTML. Can you please post a representative sample of the actual HTML or a link to the source? You should be using XML operations. It's kind of sad two folks are aiding this path fraught with peril.
– hrbrmstr
Nov 14 '18 at 2:46
add a comment |
2 Answers
2
active
oldest
votes
Try using str_extract_all
from the stringr
library:
str_extract_all(data, "\d1,3(?:,\d3)*(?:\.\d+)?(?= people own these)")
It works nearly perfectly, returns a large list of empty characters and the value i'm after. Is there any reason it seems to return a bunch of listed NA's as well?
– Permafrost
Nov 14 '18 at 3:02
2
@Permafrost: How could anyone know the answer to that question, given the lack of any example?
– 42-
Nov 14 '18 at 3:09
@Permafrost I'm not in front of an R console at the moment, but I would suggest starting with a smaller test text, and see if you can do some debugging.
– Tim Biegeleisen
Nov 14 '18 at 3:11
Thanks very much, I was just after a quick fix so I used 'str_extract' to return a character vector not a list, then used x[!is.na(x)] (where x is the character vector) to remove the NA's and get the final value. I'll keep reading up on Xpath, but this is a fast way to get what I need.
– Permafrost
Nov 14 '18 at 4:31
add a comment |
Could you please try following.
val <- "742,811 people own these"
gsub(' [a-zA-Z]+',"",val)
Output will be as follows.
[1] "742,811"
Explanation: using gsub(global substitution) function of R here. Putting condition here where it should replace all occurrences of space with small or capital alphabets with NULL for variable val
.
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%2f53292264%2fextract-numerical-value-before-a-string-in-r%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
Try using str_extract_all
from the stringr
library:
str_extract_all(data, "\d1,3(?:,\d3)*(?:\.\d+)?(?= people own these)")
It works nearly perfectly, returns a large list of empty characters and the value i'm after. Is there any reason it seems to return a bunch of listed NA's as well?
– Permafrost
Nov 14 '18 at 3:02
2
@Permafrost: How could anyone know the answer to that question, given the lack of any example?
– 42-
Nov 14 '18 at 3:09
@Permafrost I'm not in front of an R console at the moment, but I would suggest starting with a smaller test text, and see if you can do some debugging.
– Tim Biegeleisen
Nov 14 '18 at 3:11
Thanks very much, I was just after a quick fix so I used 'str_extract' to return a character vector not a list, then used x[!is.na(x)] (where x is the character vector) to remove the NA's and get the final value. I'll keep reading up on Xpath, but this is a fast way to get what I need.
– Permafrost
Nov 14 '18 at 4:31
add a comment |
Try using str_extract_all
from the stringr
library:
str_extract_all(data, "\d1,3(?:,\d3)*(?:\.\d+)?(?= people own these)")
It works nearly perfectly, returns a large list of empty characters and the value i'm after. Is there any reason it seems to return a bunch of listed NA's as well?
– Permafrost
Nov 14 '18 at 3:02
2
@Permafrost: How could anyone know the answer to that question, given the lack of any example?
– 42-
Nov 14 '18 at 3:09
@Permafrost I'm not in front of an R console at the moment, but I would suggest starting with a smaller test text, and see if you can do some debugging.
– Tim Biegeleisen
Nov 14 '18 at 3:11
Thanks very much, I was just after a quick fix so I used 'str_extract' to return a character vector not a list, then used x[!is.na(x)] (where x is the character vector) to remove the NA's and get the final value. I'll keep reading up on Xpath, but this is a fast way to get what I need.
– Permafrost
Nov 14 '18 at 4:31
add a comment |
Try using str_extract_all
from the stringr
library:
str_extract_all(data, "\d1,3(?:,\d3)*(?:\.\d+)?(?= people own these)")
Try using str_extract_all
from the stringr
library:
str_extract_all(data, "\d1,3(?:,\d3)*(?:\.\d+)?(?= people own these)")
edited Nov 14 '18 at 2:32
answered Nov 14 '18 at 2:26
Tim BiegeleisenTim Biegeleisen
224k1391143
224k1391143
It works nearly perfectly, returns a large list of empty characters and the value i'm after. Is there any reason it seems to return a bunch of listed NA's as well?
– Permafrost
Nov 14 '18 at 3:02
2
@Permafrost: How could anyone know the answer to that question, given the lack of any example?
– 42-
Nov 14 '18 at 3:09
@Permafrost I'm not in front of an R console at the moment, but I would suggest starting with a smaller test text, and see if you can do some debugging.
– Tim Biegeleisen
Nov 14 '18 at 3:11
Thanks very much, I was just after a quick fix so I used 'str_extract' to return a character vector not a list, then used x[!is.na(x)] (where x is the character vector) to remove the NA's and get the final value. I'll keep reading up on Xpath, but this is a fast way to get what I need.
– Permafrost
Nov 14 '18 at 4:31
add a comment |
It works nearly perfectly, returns a large list of empty characters and the value i'm after. Is there any reason it seems to return a bunch of listed NA's as well?
– Permafrost
Nov 14 '18 at 3:02
2
@Permafrost: How could anyone know the answer to that question, given the lack of any example?
– 42-
Nov 14 '18 at 3:09
@Permafrost I'm not in front of an R console at the moment, but I would suggest starting with a smaller test text, and see if you can do some debugging.
– Tim Biegeleisen
Nov 14 '18 at 3:11
Thanks very much, I was just after a quick fix so I used 'str_extract' to return a character vector not a list, then used x[!is.na(x)] (where x is the character vector) to remove the NA's and get the final value. I'll keep reading up on Xpath, but this is a fast way to get what I need.
– Permafrost
Nov 14 '18 at 4:31
It works nearly perfectly, returns a large list of empty characters and the value i'm after. Is there any reason it seems to return a bunch of listed NA's as well?
– Permafrost
Nov 14 '18 at 3:02
It works nearly perfectly, returns a large list of empty characters and the value i'm after. Is there any reason it seems to return a bunch of listed NA's as well?
– Permafrost
Nov 14 '18 at 3:02
2
2
@Permafrost: How could anyone know the answer to that question, given the lack of any example?
– 42-
Nov 14 '18 at 3:09
@Permafrost: How could anyone know the answer to that question, given the lack of any example?
– 42-
Nov 14 '18 at 3:09
@Permafrost I'm not in front of an R console at the moment, but I would suggest starting with a smaller test text, and see if you can do some debugging.
– Tim Biegeleisen
Nov 14 '18 at 3:11
@Permafrost I'm not in front of an R console at the moment, but I would suggest starting with a smaller test text, and see if you can do some debugging.
– Tim Biegeleisen
Nov 14 '18 at 3:11
Thanks very much, I was just after a quick fix so I used 'str_extract' to return a character vector not a list, then used x[!is.na(x)] (where x is the character vector) to remove the NA's and get the final value. I'll keep reading up on Xpath, but this is a fast way to get what I need.
– Permafrost
Nov 14 '18 at 4:31
Thanks very much, I was just after a quick fix so I used 'str_extract' to return a character vector not a list, then used x[!is.na(x)] (where x is the character vector) to remove the NA's and get the final value. I'll keep reading up on Xpath, but this is a fast way to get what I need.
– Permafrost
Nov 14 '18 at 4:31
add a comment |
Could you please try following.
val <- "742,811 people own these"
gsub(' [a-zA-Z]+',"",val)
Output will be as follows.
[1] "742,811"
Explanation: using gsub(global substitution) function of R here. Putting condition here where it should replace all occurrences of space with small or capital alphabets with NULL for variable val
.
add a comment |
Could you please try following.
val <- "742,811 people own these"
gsub(' [a-zA-Z]+',"",val)
Output will be as follows.
[1] "742,811"
Explanation: using gsub(global substitution) function of R here. Putting condition here where it should replace all occurrences of space with small or capital alphabets with NULL for variable val
.
add a comment |
Could you please try following.
val <- "742,811 people own these"
gsub(' [a-zA-Z]+',"",val)
Output will be as follows.
[1] "742,811"
Explanation: using gsub(global substitution) function of R here. Putting condition here where it should replace all occurrences of space with small or capital alphabets with NULL for variable val
.
Could you please try following.
val <- "742,811 people own these"
gsub(' [a-zA-Z]+',"",val)
Output will be as follows.
[1] "742,811"
Explanation: using gsub(global substitution) function of R here. Putting condition here where it should replace all occurrences of space with small or capital alphabets with NULL for variable val
.
answered Nov 14 '18 at 2:25
RavinderSingh13RavinderSingh13
27.4k41538
27.4k41538
add a comment |
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%2f53292264%2fextract-numerical-value-before-a-string-in-r%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
1
Please do not use regular expressions to work with HTML. Can you please post a representative sample of the actual HTML or a link to the source? You should be using XML operations. It's kind of sad two folks are aiding this path fraught with peril.
– hrbrmstr
Nov 14 '18 at 2:46