Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string contains at least one numeric character in R [duplicate]

Tags:

regex

r

I have tried the following, however, it goes wrong when the string contains any other character, say a space. As you can see below, there is a string called "subway 10", which does contain numeric characters, however, it is reported as false because of the space.

My string may contain any other character, but if it contains at least a single digit, I would like to get the indices of those strings from the array.

> mywords<- c("harry","met","sally","subway 10","1800Movies","12345")
> numbers <- grepl("^[[:digit:]]+$", mywords) 
> letters <- grepl("^[[:alpha:]]+$", mywords) 
> both <- grepl("^[[:digit:][:alpha:]]+$", mywords) 
> 
> mywords[xor((letters | numbers), both)] # letters & numbers mixed 
[1] "1800Movies"
like image 359
saltandwater Avatar asked Oct 28 '15 14:10

saltandwater


1 Answers

using \\d works for me:

grepl("\\d", mywords)
[1] FALSE FALSE FALSE  TRUE  TRUE  TRUE

so does [[:digit:]]:

grepl("[[:digit:]]", mywords)
[1] FALSE FALSE FALSE  TRUE  TRUE  TRUE

As @nrussel mentionned, you're testing if the strings contain only digits between the beginning ^ of the string till the end $.

You could also check if the strings contain something else than letters, using ^ inside brackets to negate the letters, but then "something else" is not only digits:

grepl("[^a-zA-Z]", mywords)
[1] FALSE FALSE FALSE  TRUE  TRUE  TRUE
like image 106
Cath Avatar answered Sep 19 '22 00:09

Cath