Hello everyone, I am stuck with an apparently simple issue : i) I have two sets S1 and S2, each containing a large number of integers, say zip codes. ii) Now, I just want to test whether a particular zip code belong to S1 or S2 or neither of them. iii) If it belongs to S1, the area/region gets a particular label, say 1; if it belongs to S2, it gets a label 2 and if it doesnot belong to either S1 or S2, it gets a label 0. iv) I have to do this for a large number of zip codes as well. So I was thinking of embedding it into a if else loop but I am stuck on how to define the "belongs to" in R syntax. Any hint will be much appreciated. Best regards, Dhiman Bhadra WPI, Worcester, MA 01609 [[alternative HTML version deleted]]
Seeliger.Curt at epamail.epa.gov
2011-Feb-02 21:04 UTC
[R] Testing whether a number belong to a set
> So I was thinking of embedding it into a if else loop but I am stuck onhow> to define the "belongs to" in R syntax. Any hint will be muchappreciated. ?ifelse # handles vectors, while if() handles single values ?'%in%' # also see match() and which() Enjoy the day, cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]
Hi, i think %in% will do exactly what you want eg. 5 %in% 1:10 Am 02.02.2011 21:49, schrieb DHIMAN BHADRA:> Hello everyone, > > I am stuck with an apparently simple issue : > > i) I have two sets S1 and S2, each containing a large number of integers, > say zip codes. > ii) Now, I just want to test whether a particular zip code belong to S1 or > S2 or neither of them. > iii) If it belongs to S1, the area/region gets a particular label, say 1; if > it belongs to S2, it gets a label 2 and if it doesnot belong to either S1 or > S2, it gets a label 0. > iv) I have to do this for a large number of zip codes as well. > > So I was thinking of embedding it into a if else loop but I am stuck on how > to define the "belongs to" in R syntax. Any hint will be much appreciated. > Best regards, > Dhiman Bhadra > > WPI, Worcester, MA 01609 > > [[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.-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790
Hi: Try this: S1 <- sample(10001:98999, 1000) S2 <- sample(10001:98999, 1000)> intersect(S1, S2) # lists have some overlap[1] 72484 82292 93508 50438 29603 28965 68063 79598 16497 43016 22119 # test list x <- sample(10001:98999, 1000) # tests first if x is in S1; if not, test whether it's in S2 res <- ifelse(x %in% S1, 1, ifelse(x %in% S2, 2, 0)) table(res) res 0 1 2 973 15 12 HTH, Dennis On Wed, Feb 2, 2011 at 12:49 PM, DHIMAN BHADRA <dhimanbhadra@gmail.com>wrote:> Hello everyone, > > I am stuck with an apparently simple issue : > > i) I have two sets S1 and S2, each containing a large number of integers, > say zip codes. > ii) Now, I just want to test whether a particular zip code belong to S1 or > S2 or neither of them. > iii) If it belongs to S1, the area/region gets a particular label, say 1; > if > it belongs to S2, it gets a label 2 and if it doesnot belong to either S1 > or > S2, it gets a label 0. > iv) I have to do this for a large number of zip codes as well. > > So I was thinking of embedding it into a if else loop but I am stuck on how > to define the "belongs to" in R syntax. Any hint will be much appreciated. > Best regards, > Dhiman Bhadra > > WPI, Worcester, MA 01609 > > [[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. >[[alternative HTML version deleted]]
Dhiman - You don't say what you want the result to be if the value is in both S1 and S2, but here's an example that may give you some ideas:> S1 = c('one','two','three') > S2 = c('four','five','six') > test = c('one','two','three','four','five','six','seven') > test %in% S1 + 2 * test %in% S2[1] 1 1 1 2 2 2 0 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 2 Feb 2011, DHIMAN BHADRA wrote:> Hello everyone, > > I am stuck with an apparently simple issue : > > i) I have two sets S1 and S2, each containing a large number of integers, > say zip codes. > ii) Now, I just want to test whether a particular zip code belong to S1 or > S2 or neither of them. > iii) If it belongs to S1, the area/region gets a particular label, say 1; if > it belongs to S2, it gets a label 2 and if it doesnot belong to either S1 or > S2, it gets a label 0. > iv) I have to do this for a large number of zip codes as well. > > So I was thinking of embedding it into a if else loop but I am stuck on how > to define the "belongs to" in R syntax. Any hint will be much appreciated. > Best regards, > Dhiman Bhadra > > WPI, Worcester, MA 01609 > > [[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. >