[R] grep won't work finding one column

John McKown john.archie.mckown at gmail.com
Tue Oct 14 17:08:47 CEST 2014


You're right. I don't use regexps in R very much. In most other
languages, a single \ is needed. The R parser is different and I
forgot. Thanks for the heads up.

On Tue, Oct 14, 2014 at 10:01 AM, Ivan Calandra
<ivan.calandra at univ-reims.fr> wrote:
> 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.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



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

Maranatha! <><
John McKown



More information about the R-help mailing list