Shelby McIntyre
2012-Jul-31 00:19 UTC
[R] How can I parse this string to extract just the number 11?
Below is the string to parse and return the embedded number = "11" string = "\t\t\t\t\t<span class=\"compliment-count profile\">11</span>" On Jul 29, 2012, at 3:00 AM, r-help-request@r-project.org wrote:> char[[alternative HTML version deleted]]
Rui Barradas
2012-Jul-31 07:31 UTC
[R] How can I parse this string to extract just the number 11?
Hello,
Try the following.
string <- "\t\t\t\t\t<span class=\"compliment-count
profile\">11</span>"
gsub("[^[:digit:]]", "", string)
Then use as.numeric or as.integer.
Hope this helps,
Rui Barradas
Em 31-07-2012 01:19, Shelby McIntyre escreveu:> Below is the string to parse and return the embedded number =
"11"
>
> string = "\t\t\t\t\t<span class=\"compliment-count
profile\">11</span>"
>
>
>
>
>
>
> On Jul 29, 2012, at 3:00 AM, r-help-request at r-project.org wrote:
>
>> char
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,
Try this:
?string = "\t\t\t\t\t<span class=\"compliment-count
profile\">11</span>"
?gsub(".*(11).*","\\1",string)
#[1] "11"
A.K.
----- Original Message -----
From: Shelby McIntyre <smcintyre at scu.edu>
To: r-help at r-project.org
Cc:
Sent: Monday, July 30, 2012 8:19 PM
Subject: [R] How can I parse this string to extract just the number 11?
Below is the string to parse and return the embedded number = "11"
string = "\t\t\t\t\t<span class=\"compliment-count
profile\">11</span>"
On Jul 29, 2012, at 3:00 AM, r-help-request at r-project.org wrote:
> char
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Rui Barradas
2012-Jul-31 21:01 UTC
[R] How can I parse this string to extract just the number 11?
Hello,
Sorry, but I don't understand, there's nothing specific in the search
pattern, it searches for the negation of digits and replaces them for
"".
string = "\t\t\t\t\t<span class=\"compliment-count
profile\">11</span>"
string = c(string, "\t\t\t\t\t<span class=\"compliment-count
profile\">1234</span>")
string = c(string, "\t\t\t\t\t<span class=\"compliment-count
profile\">9876543210</span>")
gsub("[^[:digit:]]", "", string)
[1] "11" "1234" "9876543210"
Have you tried with other numbers?
Rui Barradas
Em 31-07-2012 21:25, Shelby McIntyre escreveu:> Good solution for when the number is "11", however, I don't
know that the number is going to be "11", next time
> it might be 1231 or 1,254 or some other number. So, this isn't really
a solution for my situation.
>
> On Jul 31, 2012, at 12:31 AM, Rui Barradas wrote:
>
>> Hello,
>>
>> Try the following.
>>
>> string <- "\t\t\t\t\t<span class=\"compliment-count
profile\">11</span>"
>> gsub("[^[:digit:]]", "", string)
>>
>> Then use as.numeric or as.integer.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Em 31-07-2012 01:19, Shelby McIntyre escreveu:
>>> Below is the string to parse and return the embedded number =
"11"
>>>
>>> string = "\t\t\t\t\t<span class=\"compliment-count
profile\">11</span>"
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Jul 29, 2012, at 3:00 AM, r-help-request at r-project.org wrote:
>>>
>>>> char
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.