[R] grep won't work finding one column

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Oct 14 16:57:16 CEST 2014


Your question is missing a reproducible example, and you don't say how it does not work, so we cannot tell what is going on.

Two things do come to mind, though.

A) Data frame subsets with only one column by default return a vector, which is a different type of object than a single-column data frame. You would need to read ?"[.data.frame" about the "drop" argument if you wanted to consistently get a data frame from this expression.

B) The period is a wildcard in regular expressions. If you expect to limit your search to literal ".at" at the end of the name then you should use the search pattern  "\\.at$" instead (the first slash allows the second one to be stored by R in the string, and the second one is the only one seen by grep, which it reads as making the period not act like a wildcard). You really should read about regular expressions before using them. There are many tutorials on the web about this topic.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On October 14, 2014 7:23:55 AM PDT, 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!
>
>______________________________________________
>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.



More information about the R-help mailing list