[R] Targeting the elements that satisfy matching rules excluding the NA

Gabor Grothendieck ggrothendieck at myway.com
Wed Jan 5 21:26:46 CET 2005


Giovanni Malerba <bobo <at> medgen.univr.it> writes:

: 
: Dear R-List,
: here my problem:
: > a <- c("gio","gao","geo",NA,"1","alpha")
: > b <- 1:6
: > data.frame(V1=a,V2=b) -> c
: > c
:      V1 V2
: 1   gio  1
: 2   gao  2
: 3   geo  3
: 4  <NA>  4
: 5     1  5
: 6 alpha  6
: 
: > rownames(c) %in% grep("a",as.character(c$V1))
: [1] FALSE  TRUE FALSE FALSE FALSE  TRUE
: 
: while I would like to obtain
: [1] FALSE  TRUE FALSE <NA> FALSE  TRUE
: 
: Is there a simple way to do this without doing things like
: > rownames(c) %in% grep("a",as.character(c$V1)) -> tmp
: > tmp[is.na(c$V1)]<- NA
: ?
: 
: It would be nice if grep (or another command) produced FALSE or TRUE 
according 
: to the matching rule and NA if the value is NA. I was not able to find such 
a 
: feature (just to do this in one simple command-line :)).
: 


regexpr("a", as.character(c$V1)) > 0




More information about the R-help mailing list