Chris Conner
2011-Oct-01 06:00 UTC
[R] Returning vector of values shared across 3 vectors?
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. Michael Weylandt
2011-Oct-01 06:09 UTC
[R] Returning vector of values shared across 3 vectors?
Try this: shared <- vec3[ (vec3 %in% vec1) & (vec3 %in% vec2)] Michael 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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >
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>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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?