Hi all, How to convert following simple python script to R if x in a_list: print x OR simply, how to test if a vector contain a value? Thank you in advance, Hyunchul [[alternative HTML version deleted]]
Try this: is.element(2, c(1, 2, 4)) On Mon, Aug 16, 2010 at 10:06 AM, Hyunchul Kim <hyunchul.kim.sfc@gmail.com>wrote:> Hi all, > > How to convert following simple python script to R > > > if x in a_list: > print x > > OR > > simply, how to test if a vector contain a value? > > > Thank you in advance, > > Hyunchul > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
> mylist = c(1,2,3)> if (any(mylist == 3)) { print(3) } [1] 3 > On 08/16/2010 03:06 PM, Hyunchul Kim wrote:> Hi all, > > How to convert following simple python script to R > > > if x in a_list: > print x > > OR > > simply, how to test if a vector contain a value? > > > Thank you in advance, > > Hyunchul > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Mon, Aug 16, 2010 at 2:06 PM, Hyunchul Kim <hyunchul.kim.sfc at gmail.com> wrote:> Hi all, > > How to convert following simple python script to R > > > if x in a_list: > ? ?print x > > OR > > simply, how to test if a vector contain a value?if(any(a_list == x)){ print(x) } Or use %in%:> a_list=c(1,2,3,4) > b_list=c(1,2,4) > 3 %in% a_list[1] TRUE> 3 %in% b_list[1] FALSE Or use "match" if you want to know where in a_list it is. Any simple beginners guide to R should tell you about these. Barry
You have a lot of different way a<-c(1,2,3,NA,3,4,3) is.na(a) [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE a==2 [1] FALSE TRUE FALSE NA FALSE FALSE FALSE Look at here: http://cran.r-project.org/doc/manuals/R-intro.html -- View this message in context: http://r.789695.n4.nabble.com/how-to-test-if-a-vector-contain-a-value-tp2326843p2326868.html Sent from the R help mailing list archive at Nabble.com.
Hi Hyunchul, See ?match -Ista On Mon, Aug 16, 2010 at 1:06 PM, Hyunchul Kim <hyunchul.kim.sfc at gmail.com> wrote:> Hi all, > > How to convert following simple python script to R > > > if x in a_list: > ? ?print x > > OR > > simply, how to test if a vector contain a value? > > > Thank you in advance, > > Hyunchul > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org