0

I'm trying to extract the indices of a large matrix on the basis of grepping two separate strings.

An example matrix might look like:

a=as.matrix(c("a","b","c"))
a=cbind(a,c("yes", "no", "maybe"))
rownames(a)=c("one", "two","three")
colnames(a)=c("letter", "status")

Both of these work:

grep("letter", colnames(a))
grep("status", colnames(a))

I'd like this to work, but it doesn't:

grep("letter"|"status", colnames(a))

Is the easiest way to collect both greps just concatenating them into a vector, or is there something we can do within the grep function?:

cols_to_get= c(grep("letter", colnames(a)), grep("status", colnames(a)))
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Atticus29
  • 4,190
  • 18
  • 47
  • 84
  • 3
    does this help? http://stackoverflow.com/questions/33695462/r-filter-a-column-which-contains-several-keywords – rawr Mar 30 '16 at 19:24
  • 4
    I think what @rawr is indicating is that this has already been asked before, and that in your case `grep("letter|status",colnames(a))` would work. – RHertel Mar 30 '16 at 19:27

1 Answers1

3

Use proper regex? grep("letter|status", colnames(a))

gwatson
  • 478
  • 2
  • 8