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

David Winsemius dwinsemius at comcast.net
Tue Apr 24 16:07:33 CEST 2012


On Apr 24, 2012, at 9:40 AM, Paul Miller wrote:

> 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() :
>  'x' must be numeric

The error message strongly suggests at least one non-numeric column.  
What does this return:

lapply( KRASyn[grep("(?!muta)non|un", names(KRASyn), perl=TRUE)],
               is.numeric)

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list