Finding common an unique elements in character vectors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding common an unique elements in character vectors

Thomas Jensen-6
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding common an unique elements in character vectors

Henrique Dallazuanna
Try this:

setdiff(vector_1, vector_2)

On Mon, Mar 29, 2010 at 2:53 PM, Thomas Jensen
<[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding common an unique elements in character vectors

Bert Gunter
In reply to this post by Thomas Jensen-6
?setdiff

Bert Gunter
Genentech Nonclinical Biostatistics
 
 

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On
Behalf Of Thomas Jensen
Sent: Monday, March 29, 2010 10:53 AM
To: [hidden email]
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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding common an unique elements in character vectors

Stephan Kolassa
In reply to this post by Thomas Jensen-6
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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding common an unique elements in character vectors

Chuck Cleland
In reply to this post by Thomas Jensen-6
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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding common an unique elements in character vectors

David Winsemius
In reply to this post by Thomas Jensen-6

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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.