[R] grep won't work finding one column

John McKown john.archie.mckown at gmail.com
Tue Oct 14 16:38:01 CEST 2014


On Tue, Oct 14, 2014 at 9:23 AM, Kate Ignatius <kate.ignatius at gmail.com> wrote:
> I'm having an issue with grep:
>
> I have numerous columns that end with .at... when I use grep like so:
>
> df[,grep(".at",colnames(df))]
>
> it works fine.  When I have one column that ends with .at, it does not
> work.  Why is that?  As this is loop with varying number of columns
> ending in .at I would like some code that would work with 1 to n
> number of columns.
>
> Is there something more optimal than grep?
>
> Thanks!

I can't answer your direct question. But do you realize that your code
does not match your words? The grep show does not _only_ match columns
who name end with the characters '.at'. It matches all column names
which contain any character followed by the characters "at". To do the
match with only columns whose names end with the characters ".at", you
need: grep("\.at$",colnames(df)).

You might want to post an example which fails. Just to be complete, be
sure to use the dput() function so that it is easy for members of the
group to cut'n'paste to get your data into our own R workspace.

-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown



More information about the R-help mailing list