[R] grep won't work finding one column

Ivan Calandra ivan.calandra at univ-reims.fr
Tue Oct 14 17:01:37 CEST 2014


Shouldn't it be
grep("\\.at$",colnames(df))
with double back slash?

Ivan

--
Ivan Calandra
University of Reims Champagne-Ardenne
GEGENA² - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr
https://www.researchgate.net/profile/Ivan_Calandra

Le 14/10/14 16:38, John McKown a écrit :
> 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.
>



More information about the R-help mailing list