Hello everyone, I have a question which is probably rooted in my lack of understanding when it comes to math. I just did the following: v <- c(1:20) w <- c(11:30) setdiff(v, w) and got: 1 2 3 4 5 6 7 8 9 10 Then I did the following: setdiff(w, v) and got, not surprisingly: 21 22 23 24 25 26 27 28 29 30 Now I was originally expecting to get bot with the first call of setdiff(v, w) and couldn't find any reason not to expect this from ?setdiff() Am I missing somethin vital here or does setdiff() always give me the elements of the first set that are not in the second one and not those which are exclusive to either one, just dropping the ones in the intersection of both sets? Many Thanks in advance Raphael
Hi, Please check this link: http://r.789695.n4.nabble.com/meaning-of-asymmetric-on-help-page-for-intersect-td877408.html union(setdiff(v,w), setdiff(w,v)) #or in this case setdiff(union(v,w),intersect(v,w)) #or ?setdiff(c(v,w),c(v,w)[duplicated(c(v,w))]) A.K. .paebst at gmail.com> wrote: Hello everyone, I have a question which is probably rooted in my lack of understanding when it comes to math. I just did the following: v <- c(1:20) w <- c(11:30) setdiff(v, w) and got: 1 2 3 4 5 6 7 8 9 10 Then I did the following: setdiff(w, v) and got, not surprisingly: 21 22 23 24 25 26 27 28 29 30 Now I was originally expecting to get bot with the first call of setdiff(v, w) and couldn't find any reason not to expect this from ?setdiff() Am I missing somethin vital here or does setdiff() always give me the elements of the first set that are not in the second one and not those which are exclusive to either one, just dropping the ones in the intersection of both sets? Many Thanks in advance Raphael ______________________________________________ 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.
About time for you to adjust your expectations... looks right to me from both a mathematical sense and as the functions are designed. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On June 1, 2014 11:57:42 PM PDT, "Raphael P?bst" <raphael.paebst at gmail.com> wrote:>Hello everyone, I have a question which is probably rooted in my lack >of understanding when it comes to math. > >I just did the following: > >v <- c(1:20) >w <- c(11:30) >setdiff(v, w) > >and got: >1 2 3 4 5 6 7 8 9 10 > >Then I did the following: >setdiff(w, v) >and got, not surprisingly: >21 22 23 24 25 26 27 28 29 30 > >Now I was originally expecting to get bot with the first call of >setdiff(v, w) and couldn't find any reason not to expect this from >?setdiff() > >Am I missing somethin vital here or does setdiff() always give me the >elements of the first set that are not in the second one and not those >which are exclusive to either one, just dropping the ones in the >intersection of both sets? > >Many Thanks in advance > >Raphael > >______________________________________________ >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.
Hello,>From the help page: "Performs *set* union, intersection, (asymmetric!)difference, equality and membership on two vectors." Hope this helps, Pascal On Mon, Jun 2, 2014 at 3:57 PM, Raphael P?bst <raphael.paebst at gmail.com> wrote:> Hello everyone, I have a question which is probably rooted in my lack > of understanding when it comes to math. > > I just did the following: > > v <- c(1:20) > w <- c(11:30) > setdiff(v, w) > > and got: > 1 2 3 4 5 6 7 8 9 10 > > Then I did the following: > setdiff(w, v) > and got, not surprisingly: > 21 22 23 24 25 26 27 28 29 30 > > Now I was originally expecting to get bot with the first call of > setdiff(v, w) and couldn't find any reason not to expect this from > ?setdiff() > > Am I missing somethin vital here or does setdiff() always give me the > elements of the first set that are not in the second one and not those > which are exclusive to either one, just dropping the ones in the > intersection of both sets? > > Many Thanks in advance > > Raphael > > ______________________________________________ > 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.-- Pascal Oettli Project Scientist JAMSTEC Yokohama, Japan
There are two kinds of set difference: the usual set difference <https://en.wikipedia.org/wiki/Set_difference#Relative_complement>, also called the asymmetric difference or relative complement, and the symmetric difference <https://en.wikipedia.org/wiki/Symmetric_difference>. In R, setdiff(a,b) is the usual asymmetric set difference, that is, all the elements of a which are not elements of b. It is the set equivalent of the logical expression (a and not b). The symmetric difference consists of all the elements which appear in either a or b, but not both. It is the set equivalent of logical xor. I don't believe there is a standard R function for the symmetric difference, but you can calculate it as symdiff <- function (a,b) setdiff(union(a,b),intersect(a,b)) or symdiff <- function(a,b) unique( c( a[is.na(match(a,b))], b[is.na(match(b,a))] ) ) Challenge: is there a shorter or more efficient way to calculate symdiff using R primitives? -s On Mon, Jun 2, 2014 at 2:57 AM, Raphael Päbst <raphael.paebst@gmail.com> wrote:> Hello everyone, I have a question which is probably rooted in my lack > of understanding when it comes to math. > > I just did the following: > > v <- c(1:20) > w <- c(11:30) > setdiff(v, w) > > and got: > 1 2 3 4 5 6 7 8 9 10 > > Then I did the following: > setdiff(w, v) > and got, not surprisingly: > 21 22 23 24 25 26 27 28 29 30 > > Now I was originally expecting to get bot with the first call of > setdiff(v, w) and couldn't find any reason not to expect this from > ?setdiff() > > Am I missing somethin vital here or does setdiff() always give me the > elements of the first set that are not in the second one and not those > which are exclusive to either one, just dropping the ones in the > intersection of both sets? > > Many Thanks in advance > > Raphael > > ______________________________________________ > 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]]