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?
Can you say a little more about what you mean it does not work? I'd guess
you have a regular expression mistake and are probably getting more columns
than desired, but without an example, it's hard to be certain.
Use dput() and head() to give a small cut-and-paste-able example.
Michael
On Mon,
Sorry, my mistake. The thing is that the command return no results at
all. However, when I just tried a simpler version of this (I had no
capital letters or no spaces in the string), it worked fine. I cant
figure it out, I think it all boils down to the fact that I'm no
expert at regexp's...
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))]
HTH,
Dennis
On Mon, Aug
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))]
5 matches
Mail list logo