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"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With