Re: [R] test if elements of a character vector contain letters

2012-08-08 Thread Liviu Andronic
On Tue, Aug 7, 2012 at 10:26 PM, Marc Schwartz wrote: > since there are alpha-numerics present, whereas the first option will: > >> grepl("[^[:alnum:]]", "ab%") > [1] TRUE > > > So, use the first option. > And I should start reading more carefully. The above works fine for me. I ended up defining

Re: [R] test if elements of a character vector contain letters

2012-08-07 Thread Liviu Andronic
On Tue, Aug 7, 2012 at 10:18 PM, Marc Schwartz wrote: > 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 usin

Re: [R] test if elements of a character vector contain letters

2012-08-07 Thread Marc Schwartz
On Aug 7, 2012, at 3:18 PM, Marc Schwartz wrote: > > On Aug 7, 2012, at 3:02 PM, Liviu Andronic wrote: > >> On Mon, Aug 6, 2012 at 7:35 PM, Marc Schwartz wrote: >>> is.letter <- function(x) grepl("[[:alpha:]]", x) >>> is.number <- function(x) grepl("[[:digit:]]", x) >>> >> >> Another follo

Re: [R] test if elements of a character vector contain letters

2012-08-07 Thread Marc Schwartz
On Aug 7, 2012, at 3:02 PM, Liviu Andronic wrote: > On Mon, Aug 6, 2012 at 7:35 PM, Marc Schwartz 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:

Re: [R] test if elements of a character vector contain letters

2012-08-07 Thread R. Michael Weylandt
On Tue, Aug 7, 2012 at 4:28 AM, Liviu Andronic wrote: > On Mon, Aug 6, 2012 at 7:35 PM, Marc Schwartz wrote: >> is.letter <- function(x) grepl("[[:alpha:]]", x) >> is.number <- function(x) grepl("[[:digit:]]", x) >> > Quick follow-up question. > > I'm always reluctant to create functions that wou

Re: [R] test if elements of a character vector contain letters

2012-08-07 Thread Liviu Andronic
On Mon, Aug 6, 2012 at 7:35 PM, Marc Schwartz 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[

Re: [R] test if elements of a character vector contain letters

2012-08-07 Thread Liviu Andronic
On Mon, Aug 6, 2012 at 7:35 PM, Marc Schwartz wrote: > is.letter <- function(x) grepl("[[:alpha:]]", x) > is.number <- function(x) grepl("[[:digit:]]", x) > Quick follow-up question. I'm always reluctant to create functions that would resemble the method of a function (here, is() ), but would in

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread Liviu Andronic
On Mon, Aug 6, 2012 at 7:35 PM, Marc Schwartz wrote: > is.letter <- function(x) grepl("[[:alpha:]]", x) > is.number <- function(x) grepl("[[:digit:]]", x) > This does exactly what I wanted: > x [1] "a10" "b8" "c9" "d2" "e3" "f4" "g1" "h7" "i6" "j5" "k" "l" "m" "n" [15] "o" "p"

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread Yihui Xie
You probably mean grepl('[a-zA-Z]', x) Regards, Yihui -- Yihui Xie Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Mon, Aug 6, 2012 at 3:29 PM, Liviu Andronic wrote: > On Mon, Aug 6, 2012 at 6:42 PM, Bert Gunter wrote:

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread Liviu Andronic
On Mon, Aug 6, 2012 at 6:42 PM, Bert Gunter wrote: > nzchar(x) & !is.na(x) > > No? > It doesn't work for what I need: > x [1] "a10" "b8" "c9" "d2" "e3" "f4" "g1" "h7" "i6" "j5" "k" "l" "m" "n" [15] "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "1" "2" [29]

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread David L Carlson
is.letter <- function(x) grepl("[[:alpha:]]", x) > is.letter(x) [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [25] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE F

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread Marc Schwartz
On Aug 6, 2012, at 12:06 PM, Marc Schwartz wrote: > Perhaps I am missing something, but why use sapply() when grepl() is already > vectorized? > > is.letter <- function(x) grepl("[:alpha:]", x) > is.number <- function(x) grepl("[:digit:]", x) Sorry, typos in the above from my C&P. Should be:

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread arun
ot;20"  "21"  "22" [49] "23"  "24"  "25"  "26"  grepl("^[[:alpha:]][[:digit:]]",x1)  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FA

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread Marc Schwartz
Perhaps I am missing something, but why use sapply() when grepl() is already vectorized? is.letter <- function(x) grepl("[:alpha:]", x) is.number <- function(x) grepl("[:digit:]", x) x <- c(letters, 1:26) x[1:10] <- paste(x[1:10], sample(1:10, 10), sep='') x <- rep(x, 1e3) > str(x) chr [1:52

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread Martin Morgan
On 08/06/2012 09:51 AM, Rui Barradas wrote: Hello, Fun as an exercise in vectorization. 30 times faster. Don't look, guess. > system.time(res0 <- grepl("[[:alpha:]]", x)) user system elapsed 0.060 0.000 0.061 > system.time(res1 <- has_letter(x)) user system elapsed 3.728 0.00

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread Rui Barradas
Hello, Fun as an exercise in vectorization. 30 times faster. Don't look, guess. Gave it up? Ok, here it is. is_letter <- function(x, pattern=c(letters, LETTERS)){ sapply(x, function(y){ any(sapply(pattern, function(z) grepl(z, y, fixed=T))) }) } # test ascii codes, just one loo

Re: [R] test if elements of a character vector contain letters

2012-08-06 Thread Bert Gunter
nzchar(x) & !is.na(x) No? -- Bert On Mon, Aug 6, 2012 at 9:25 AM, Liviu Andronic wrote: > Dear all > I'm pretty sure that I'm approaching the problem in a wrong way. > Suppose the following character vector: >> (x[1:10] <- paste(x[1:10], sample(1:10, 10), sep='')) > [1] "a10" "b7" "c2" "d3"

[R] test if elements of a character vector contain letters

2012-08-06 Thread Liviu Andronic
Dear all I'm pretty sure that I'm approaching the problem in a wrong way. Suppose the following character vector: > (x[1:10] <- paste(x[1:10], sample(1:10, 10), sep='')) [1] "a10" "b7" "c2" "d3" "e6" "f1" "g5" "h8" "i9" "j4" > x [1] "a10" "b7" "c2" "d3" "e6" "f1" "g5" "h8" "i9" "j