[R] grep won't work finding one column
Kate Ignatius
kate.ignatius at gmail.com
Tue Oct 14 17:09:28 CEST 2014
In the sense - it does not work. it works when there are 50 samples
in the file, but it does not work when there is one.
The usual headings are: sample1.at sample1.dp
sample1.fg sample2.at sample2.dp sample2.fg.... and so on to a max of
sample50.at sample50.dp sample50.fg
using this greps out all the .at columns perfectly:
df[,grep(".at",colnames(df))]
When I come across a file when there is one sample:
sample1.at sample1.dp sample1.fg
Using this:
df[,grep(".at",colnames(df))]
returns nothing.
Oh - AT/at was just an example... thats not my problem...
On Tue, Oct 14, 2014 at 10:57 AM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
> 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