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

Petr Pikal petr.pikal at precheza.cz
Thu Jan 6 14:18:40 CET 2005


Hi Giovanni

I am not sure if it is the correct way but

regexpr("a",as.character(df$V1))>0
[1] FALSE  TRUE FALSE    NA FALSE  TRUE

gives me the logical vector you would like to see.

Cheers
Petr

On 5 Jan 2005 at 19:16, Giovanni Malerba wrote:

> 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 :)).
> 
> Thank you,
> Giovanni Malerba.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list