On Aug 7, 2012, at 3:02 PM, Liviu Andronic <landronim...@gmail.com> wrote:
> On Mon, Aug 6, 2012 at 7:35 PM, Marc Schwartz <marc_schwa...@me.com> wrote: >> is.letter <- function(x) grepl("[[:alpha:]]", x) >> is.number <- function(x) grepl("[[:digit:]]", x) >> > > Another follow-up. To test for (non-)alphanumeric one would do the following: >> x <- c(letters, 1:26, '+', '-', '%^&') >> x[1:10] <- paste(x[1:10], 1:10, sep='') >> x > [1] "a1" "b2" "c3" "d4" "e5" "f6" "g7" "h8" "i9" "j10" "k" > "l" "m" "n" > [15] "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" > "z" "1" "2" > [29] "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" > "14" "15" "16" > [43] "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "+" > "-" "%^&" >> xb <- grepl("[[:alnum:]]",x) ##test for alphanumeric chars >> x[xb] > [1] "a1" "b2" "c3" "d4" "e5" "f6" "g7" "h8" "i9" "j10" "k" > "l" "m" "n" > [15] "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" > "z" "1" "2" > [29] "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" > "14" "15" "16" > [43] "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" >> xb <- grepl("[[:punct:]]",x) ##test for non-alphanumeric chars >> x[xb] > [1] "+" "-" "%^&" That will get you values where punctuation characters are used, but there may be other non-alphanumeric characters in the vector. There may be ASCII control codes, tabs, newlines, CR, LF, spaces, etc. which would not be found by using [:punct:]. For example: > grepl("[[:punct:]]", " ") [1] FALSE If you want to explicitly look for non-alphanumeric characters, you would be better off using a negation of [:alnum:] such as: grepl("[^[:alnum:]]", x) or !grepl("[[:alnum:]]", x) Regards, Marc > > More regex rules are available on the Wiki [1]. Regards > Liviu > > [1] http://en.wikipedia.org/wiki/Regular_expression > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.