[R] Use of [:alnum:] or . in gsub() etc..
Marc Schwartz
marc_schwartz at comcast.net
Fri Jan 16 17:21:17 CET 2009
on 01/16/2009 10:13 AM ppaarrkk wrote:
>
> test = c ( "AAABBB", "CCC" )
>
>
> This works :
>
> gsub ( "[A-Z]", "2", test )
>
>
>
> None of these do :
>
> gsub ( "[A-Z]", [:alnum:], test )
> gsub ( "[A-Z]", [[:alnum:]], test )
> gsub ( "[A-Z]", "[:alnum:]", test )
> gsub ( "[A-Z]", "[[:alnum:]]", test )
> gsub ( "[A-Z]", "^[:alnum:]$", test )
>
>
>
> What am I doing wrong, please ?
The syntax ordering is off. You are in effect trying to replace the
characters A-Z with the character vector "[[:alnum:]]". Thus:
> gsub ("[A-Z]", "[[:alnum:]]", test )
[1] "[[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]]"
[2] "[[:alnum:]][[:alnum:]][[:alnum:]]"
Recall the basic syntax for gsub() is:
gsub(SearchPattern, ReplacementVector, Source)
What you want is:
> gsub ("[[:alnum:]]", "2" , test )
[1] "222222" "222"
HTH,
Marc Schwartz
More information about the R-help
mailing list