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
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
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
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:
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
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[
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
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"
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:
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]
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
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:
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
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
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
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
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"
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
18 matches
Mail list logo