[R] Check if string has all alphabets or numbers

hadley wickham h.wickham at gmail.com
Mon Nov 23 15:17:34 CET 2009


>> mywords<- c("harry","met","sally","subway10","1800Movies","12345", "not correct 123")
>> all.letters <- grep("^[[:alpha:]]*$", mywords)
>> all.numbers <- grep("^[[:digit:]]*$", mywords)  # numbers
>> mixed <- grep("^[[:digit:][:alpha:]]*$", mywords)

mywords<- c("harry","met","sally","subway10","1800Movies","12345",
"not correct 123", "")
mywords[grepl("^[[:digit:][:alpha:]]*$", mywords)]

So maybe you should use

mywords[grepl("^[[:digit:][:alpha:]]+$", mywords)]


And grepl is highly recommended over grep.

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list