On Aug 22, 2011, at 1:45 PM, Dennis Murphy wrote:

Hi:

You need a leading ^ in your grep string. Here's a reproducible
example to illustrate:

df <- data.frame(Xyz1 = rnorm(5), Xyz2 = rnorm(5), Xyz3 = rnorm(5),
                 Abc1 = rnorm(5), Abc2 = rnorm(5))
df[, grep('^Xyz', names(df))]
df[, grep('^Abc', names(df))]

The leading "^" should not be necessary to solve the problem of "no matches at all" reported in the followup message, since it narrows the search rather than expanding it. It would only be necessary if there were column names that you wanted to exlcude where "Xyz" was not at the beginning of the string. My guess is that th eOP does not realize the importantce of capitaliszation and that all these are different so would NOT be matched by grep("Xyz" ,...): "xyz", xYz", "xyZ".


HTH,
Dennis

On Mon, Aug 22, 2011 at 7:33 AM, Jay <josip.2...@gmail.com> wrote:
Hi,

Let's say that I have a set of column names that begin with the string
"Xyz". How do I extract these specific columns? I tried to do the
following:

dataframe1[,grep("Xyz",colnames(dataframe1))]

But it does not work. What is wrong with my expression?


--
David Winsemius, MD
West Hartford, CT

______________________________________________
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.

Reply via email to