# [R] Returning vector of values shared across 3 vectors?

Brian Diggs diggsb at ohsu.edu
Mon Oct 3 22:07:32 CEST 2011

```On 10/1/2011 3:03 AM, jim holtman wrote:
> try this:
>
>> vec1<- c(4,5,6,7,8,9,10,11,12,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81)
>> vec2<- c (1,2,3,4,5,6,7,8,9,10,11,12,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66)
>> vec3<- c (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,52)
>>   intersect(vec1,intersect(vec2, vec3))
>   [1]  4  5  6  7  8  9 10 11 12 52
>>

Or if your real problem may go to more vec's and you don't want to keep
nesting the calls:

Reduce(intersect, list(vec1, vec2, vec3))
# [1]  4  5  6  7  8  9 10 11 12 52

>
> On Sat, Oct 1, 2011 at 2:00 AM, Chris Conner<connerpharmd at yahoo.com>  wrote:
>> Help-Rs,
>>
>> I've got three vectors representing participants:
>>
>> vec1<- c(4,5,6,7,8,9,10,11,12,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81)
>> vec2<- c (1,2,3,4,5,6,7,8,9,10,11,12,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66)
>> vec3<- c (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,52)
>>
>> I'd like to return a vector that contains only the values that are shared across ALL THREE vectors. So the statement would return a vector that looked like this:
>> 4,5,6,7,8,9,10,11,12,52
>>
>> For some reason I initially thought that a cbind and a unique() would handle it, but then common sense sunk in.  I think the sleep deprivation is starting to take it's toll.  I've got to believe that there is a simple solution to this dilema.
>>
>> Thanks in adance for any help!
>> C
>>         [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
>

--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

```