[R] How to take difference of sets when there is an empty subset involved

Duncan Murdoch murdoch.duncan at gmail.com
Sun Mar 18 17:05:08 CET 2018


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



More information about the R-help mailing list