Thomas Jensen
2010-Mar-29 17:53 UTC
[R] Finding common an unique elements in character vectors
Dear R-list, I have a problem which I think is quite basic, but so far google has not helped me. I have two vectors like this: vector_1 <- c(Belgium, Spain, Greece, Ireland, Luxembourg, Netherlands, Portugal) vector_2 <- c(Denmark, Luxembourg) I would like to find the elements in vector_1 that are not in vector_2 so that i get a vector with these countries: Belgium, Spain, Greece, Ireland, Netherlands, Portugal. Thanks a lot, Thomas
Henrique Dallazuanna
2010-Mar-29 19:19 UTC
[R] Finding common an unique elements in character vectors
Try this: setdiff(vector_1, vector_2) On Mon, Mar 29, 2010 at 2:53 PM, Thomas Jensen <thomas.jensen at eup.gess.ethz.ch> wrote:> Dear R-list, > > I have a problem which I think is quite basic, but so far google has not > helped me. > > I have two vectors like this: > > vector_1 <- c(Belgium, Spain, Greece, Ireland, Luxembourg, Netherlands, > Portugal) > > vector_2 <- c(Denmark, Luxembourg) > > I would like to find the elements in vector_1 that are not in vector_2 > > so that i get a vector with these countries: Belgium, Spain, Greece, > Ireland, Netherlands, Portugal. > > Thanks a lot, > > Thomas > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Bert Gunter
2010-Mar-29 19:29 UTC
[R] Finding common an unique elements in character vectors
?setdiff Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Thomas Jensen Sent: Monday, March 29, 2010 10:53 AM To: r-help at r-project.org Subject: [R] Finding common an unique elements in character vectors Dear R-list, I have a problem which I think is quite basic, but so far google has not helped me. I have two vectors like this: vector_1 <- c(Belgium, Spain, Greece, Ireland, Luxembourg, Netherlands, Portugal) vector_2 <- c(Denmark, Luxembourg) I would like to find the elements in vector_1 that are not in vector_2 so that i get a vector with these countries: Belgium, Spain, Greece, Ireland, Netherlands, Portugal. Thanks a lot, Thomas ______________________________________________ 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.
Stephan Kolassa
2010-Mar-29 19:29 UTC
[R] Finding common an unique elements in character vectors
Hi Thomas, %in% does the trick: vector_1 <- c("Belgium", "Spain", "Greece", "Ireland", "Luxembourg", "Netherlands","Portugal") vector_2 <- c("Denmark", "Luxembourg") vector_1[!(vector_1 %in% vector_2)] HTH, Stephan Thomas Jensen schrieb:> Dear R-list, > > I have a problem which I think is quite basic, but so far google has not > helped me. > > I have two vectors like this: > > vector_1 <- c(Belgium, Spain, Greece, Ireland, Luxembourg, Netherlands, > Portugal) > > vector_2 <- c(Denmark, Luxembourg) > > I would like to find the elements in vector_1 that are not in vector_2 > > so that i get a vector with these countries: Belgium, Spain, Greece, > Ireland, Netherlands, Portugal. > > Thanks a lot, > > Thomas > > ______________________________________________ > 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. >
Chuck Cleland
2010-Mar-29 19:30 UTC
[R] Finding common an unique elements in character vectors
On 3/29/2010 1:53 PM, Thomas Jensen wrote:> Dear R-list, > > I have a problem which I think is quite basic, but so far google has not > helped me. > > I have two vectors like this: > > vector_1 <- c(Belgium, Spain, Greece, Ireland, Luxembourg, Netherlands, > Portugal) > > vector_2 <- c(Denmark, Luxembourg) > > I would like to find the elements in vector_1 that are not in vector_2 > > so that i get a vector with these countries: Belgium, Spain, Greece, > Ireland, Netherlands, Portugal.vector_1 <- c('Belgium', 'Spain', 'Greece', 'Ireland', 'Luxembourg', 'Netherlands', 'Portugal') vector_2 <- c('Denmark', 'Luxembourg') setdiff(vector_1, vector_2) ?setdiff> Thanks a lot, > > Thomas > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
David Winsemius
2010-Mar-29 19:31 UTC
[R] Finding common an unique elements in character vectors
On Mar 29, 2010, at 1:53 PM, Thomas Jensen wrote:> Dear R-list, > > I have a problem which I think is quite basic, but so far google has > not > helped me. > > I have two vectors like this: > > vector_1 <- c(Belgium, Spain, Greece, Ireland, Luxembourg, > Netherlands, > Portugal) > > vector_2 <- c(Denmark, Luxembourg) > > I would like to find the elements in vector_1 that are not in vector_2?"%in%" # the help page for %in% also defines a %w/o% operator as a worked example.> > so that i get a vector with these countries: Belgium, Spain, Greece, > Ireland, Netherlands, Portugal. > > Thanks a lot, > > Thomas > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT