Neha Aggarwal
2018-Mar-18 15:00 UTC
[R] How to take difference of sets when there is an empty subset involved
Hello, Problem I am facing is as follows: Set A is made of 2 sets x and y x<-{"P1", "P2", "P3", "P4"} y<-{} A<-set(x,y) #A={{}, {"P1", "P2", "P3", "P4"}} i need to use A in a recursive loop where i need to take set difference of A and it 's elements. Example: for (i in A){ print(i) A<-set_symdiff(i,A) ###Imp line i need help on print(A) } the program is unable to recognise the empty set in the set A. I tried various methods in Imp line above:> A-i#Output : {{}, {"P1", "P2", "P3", "P4"}}> set_symdiff(i,A)Output: {{}, {"P1", "P2", "P3", "P4"}}> set_symdiff(A,i)Output: {{}, {"P1", "P2", "P3", "P4"}} i also tried with set_complement() .But it does not subtract the empty subset from the bigger set. It seems it doesn't recognise it. What else can I try ? any pointers/ packages will be great help. . Thanks in advance, Neha [[alternative HTML version deleted]]
Duncan Murdoch
2018-Mar-18 16:05 UTC
[R] How to take difference of sets when there is an empty subset involved
On 18/03/2018 11:00 AM, Neha Aggarwal wrote:> Hello, > > Problem I am facing is as follows: > > Set A is made of 2 sets x and y > x<-{"P1", "P2", "P3", "P4"} > y<-{}Those aren't R code. I think you meant x <- set("P1", "P2", "P3", "P4") y <- set()> A<-set(x,y) > #A={{}, {"P1", "P2", "P3", "P4"}} > > i need to use A in a recursive loop where i need to take set difference of > A and it 's elements.This doesn't really make sense. A is a set of sets. Its elements are sets of character strings. There is no intersection between A and either of its elements, so the symmetric difference is just the union. What do you really want?> Example: > for (i in A){ > print(i) > A<-set_symdiff(i,A) ###Imp line i need help on > print(A) > }That changes A each time through the loop. Did you really want that?> > the program is unable to recognise the empty set in the set A. I tried > various methods in Imp line above: >> A-i > #Output : {{}, {"P1", "P2", "P3", "P4"}} >> set_symdiff(i,A) > Output: {{}, {"P1", "P2", "P3", "P4"}} >> set_symdiff(A,i) > Output: {{}, {"P1", "P2", "P3", "P4"}} > > i also tried with set_complement() .But it does not subtract the empty > subset from the bigger set. It seems it doesn't recognise it. > What else can I try ? any pointers/ packages will be great help. >I'm not sure what you are after, but you might want something like set_symdiff(set(i), A) or A - set(i) Duncan Murdoch