On Tue, Oct 14, 2014 at 9:23 AM, Kate Ignatius <kate.ignat...@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 ______________________________________________ R-help@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.