[R] Selecting columns whose names contain "mutated" except when they also contain "non" or "un"

Paul Miller pjmiller_57 at yahoo.com
Tue Apr 24 15:40:35 CEST 2012


Hi Greg,

This is quite helpful. Not so good yet with regular expressions in general or Perl-like regular expressions. Found the help page though, and think I was able to determine how the code works as well as how I would select only instances where "muta" is preceeded by either "non" or "un".

> (tmp <- c('mutation','nonmutated','unmutated','verymutated','other'))
[1] "mutation"    "nonmutated"  "unmutated"   "verymutated" "other"      

> grep("(?<!un)(?<!non)muta", tmp, perl=TRUE)
[1] 1 4

> grep("(?!muta)non|un", tmp, perl=TRUE)
[1] 2 3

Did I get the second grep right?

If so, do you have any sense of why it seems to fail when I apply it to my data?

> KRASyn$NonMutant_comb <- rowSums(KRASyn[grep("(?!muta)non|un", names(KRASyn), perl=TRUE)])

Error in rowSums(KRASyn[grep("(?!muta)non|un", names(KRASyn), perl = TRUE)]) : 
  'x' must be numeric

Thanks,

Paul



More information about the R-help mailing list