Giovanni Malerba
2005-Jan-05 18:16 UTC
[R] Targeting the elements that satisfy matching rules excluding the NA
Dear R-List, here my problem:> a <- c("gio","gao","geo",NA,"1","alpha") > b <- 1:6 > data.frame(V1=a,V2=b) -> c > cV1 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.
Gabor Grothendieck
2005-Jan-05 20:26 UTC
[R] Targeting the elements that satisfy matching rules excluding the NA
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
Petr Pikal
2005-Jan-06 13:18 UTC
[R] Targeting the elements that satisfy matching rules excluding the NA
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.htmlPetr Pikal petr.pikal at precheza.cz