[R] Set Difference Problem

S Ellison S.Ellison at LGCGroup.com
Mon Mar 19 14:33:52 CET 2018

First, it _looks_ like you're not constructing your starting sets properly.
The sets package manual says that the set constructor is set()
There's no apparent overload for {}

Assuming your actual commands are
x<- set("P1", "P2", "P3", "P4")

There's a difference between z<-set(x,y). which correctly forms a set of sets, with two members, x and y,  and u<-set_union(x,y) which correctly returns a set identical to x, using the empty set as you'd expect.

I'm not sure what you wanted, but perhaps you wanted the union rather than the set of sets?

If you _did_ want the set of sets, z above, life is maybe more complicated than you expected. 
z is a set that contains an empty set and a populated set. The difference between that and the empty set is all of the members of z - including the empty set that is one of its members.

If you wanted something that returns a set of one set (that is, {x}, you'd have to compare z with set(y) (the latter being a non-empty set containing one member, the empty set):

> set_symdiff(z, set(y))

which correctly returns 
{{"P1", "P2", "P3", "P4"}}
Note the double parentheses ... this is still a set of sets with one member, not a set of character strings with four members.

Hope that helps ...

S Ellison

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Neha
> Aggarwal
> Sent: 18 March 2018 09:35
> To: r-help at r-project.org
> Subject: [R] Set Difference Problem
>  Hello All,
> I am facing a problem and am unable to find much help online as sets package
> in R is relatively new.
> My problem is as follows:
> Set R is made of 2 sets x and y
> x<-{"P1", "P2", "P3", "P4"}
> y<-{}
> R<-set(x,y)
> #R={{}, {"P1", "P2", "P3", "P4"}}
> i need to use R in a recursive loop where i need to take set difference of R and
> it 's elements.
> Example:
> for (r in R){
>     print(r)
>     R<-set_symdiff(r,R)
>     print(R)
> }
> the program is unable to recognise the null set in the set R. I tried various
> funtions and methods:
> > R-r
> #Output : {{}, {"P1", "P2", "P3", "P4"}}
> > set_symdiff(r,R)
> Output: {{}, {"P1", "P2", "P3", "P4"}}
> > set_symdiff(R,r)
> Output: {{}, {"P1", "P2", "P3", "P4"}}
> i also tried with set_complement() .But they do not subtract the null set from
> the bigger set. It seems they dont recognise it.
> What can I try, any pointers/ packages will be great help.
> Please help
> .
> Thanks,
> Neha
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-help mailing list