Joris Meys
2010-Apr-02 22:35 UTC
[R] compare multiple values with vector and return vector
Dear all, I have a vector, and for each element I want to check whether it is equal to any element from another vector. I want a vector of logical values with the length of the first one as return. In R this would be :> x <- 1:10 > sapply(x,function(y){any(y==c("2","3","4"))})[1] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE It works pretty smooth, but I have the feeling there's a less complicated way of doing it. My code should be readable by programmers who are not really familiar with R, but I hate to use for-loops as I have pretty huge datasets. Anybody an idea? thank you in advance. Cheers Joris -- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 Joris.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
Rolf Turner
2010-Apr-02 22:42 UTC
[R] compare multiple values with vector and return vector
On 3/04/2010, at 11:35 AM, Joris Meys wrote:> Dear all, > > I have a vector, and for each element I want to check whether it is equal to > any element from another vector. I want a vector of logical values with the > length of the first one as return. In R this would be : > >> x <- 1:10 >> sapply(x,function(y){any(y==c("2","3","4"))}) > [1] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE > > It works pretty smooth, but I have the feeling there's a less complicated > way of doing it. My code should be readable by programmers who are not > really familiar with R, but I hate to use for-loops as I have pretty huge > datasets. Anybody an idea? > thank you in advance.?"%in%" cheers, Rolf Turner ###################################################################### Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ######################################################################
mohamed.lajnef at inserm.fr
2010-Apr-02 22:55 UTC
[R] compare multiple values with vector and return vector
Hi Joris Try Is.element function: is.element (x,y) Regards mohamed Joris Meys <jorismeys at gmail.com> a ?crit?:> Dear all, > > I have a vector, and for each element I want to check whether it is equal to > any element from another vector. I want a vector of logical values with the > length of the first one as return. In R this would be : > >> x <- 1:10 >> sapply(x,function(y){any(y==c("2","3","4"))}) > [1] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE > > It works pretty smooth, but I have the feeling there's a less complicated > way of doing it. My code should be readable by programmers who are not > really familiar with R, but I hate to use for-loops as I have pretty huge > datasets. Anybody an idea? > thank you in advance. > > Cheers > Joris > > -- > Joris Meys > Statistical Consultant > > Ghent University > Faculty of Bioscience Engineering > Department of Applied mathematics, biometrics and process control > > Coupure Links 653 > B-9000 Gent > > tel : +32 9 264 59 87 > Joris.Meys at Ugent.be > ------------------------------- > Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php > > [[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. >