Reputation: 695
Basic question... I've a string and I'm trying to extract the numbers from a string using the below.
str2="<P>3.1 Design objectives .....
<Link>25
</Link> </P>"
str2
a<-strapply(str2,"\\d+",as.numeric,simplify=TRUE)
a
This returns 3
, 1
, and 25
which is fine. (It's ok that 3.1
is split up)
However, if the string has no numbers at all:
pr <- "This is a test string"
b<-strapply(pr,"\\d+",as.numeric,simplify=TRUE)
b
returns:
[[1]]
NULL
How can I use the above to detect the presence of a number(s) in a string?
is.null(b)
returned False
as it should.
length(b)
gave 1
, which is the NULL
Object?
So, is it right to say that the string does not contain any numbers if length(b)
is 1
? Is there a more elegant way of doing this?
Upvotes: 2
Views: 265
Reputation: 121568
I would unlist
the result and test its length
, For example using strapplyc
:
(ll <- as.numeric(unlist(strapplyc(str2,"\\d+",simplify = TRUE))))
numeric(0)
length(ll)
[1] 0
EDIT Rereading your question, I think it is better/safer to convert your XML structure to a simple text before appyling regular expression on it. For example you can do this :
library(XML)
toString(xmlToList(str2))
[1] "3.1 Design objectives ..... \n, 25, \n"
Upvotes: 0
Reputation: 263301
require(gsubfn)
pr <- "This is a test string"
b<-strapply(pr,"\\d+",as.numeric,simplify=TRUE)
if( !length(b[[1]]) ){TRUE}
#[1] TRUE
(This is the standard way of testing whether a list element is NULL.)
Upvotes: 4