[R] regular expression question

Peter Langfelder peter.langfelder at gmail.com
Tue Apr 12 08:11:26 CEST 2011

On Mon, Apr 11, 2011 at 10:49 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> Dear R People:
> I have a data frame with the following column names:
>> names(funky)
>  [1] "UHD.1"   "UHD.2"   "UHD.3"   "UHD.4"   "L..W..1" "L..W..2" "L..W..3"
>  [8] "L..W..4" "B..W..1" "B..W..2" "B..W..3" "B..W..4" "W..B..1" "W..B..2"
> [15] "W..B..3" "W..B..4" "B..G..1" "B..G..2" "B..G..3" "B..G..4"
> I would like to extract the letters from them; no periods, no numbers.
> I tried the following:
>> grep("[A:Z]",names(funky))
> integer(0)
> But this doesn't do it.
> I've also been experimenting with strsplit, but no luck yet.
> Any help would be much appreciated.

Not sure that this is what you mean, but here's an example of removing
all non-A-through-Z characters:

x = c("A.1", "B...2", "C..3..D");
gsub("[^(A-Z)]", "", x)
[1] "A"  "B"  "CD"

Basically you should look at the character substitution functions sub and gsub.


More information about the R-help mailing list