How to check if a set is a subset of another set in R?

R ProgrammingServer Side ProgrammingProgramming

To check if a set is a subset of another set, we can use all function with %in% operator. For example, if we have two vectors say x and y and we want to check whether x is a subset of y then we can use the command given below −

all(x %in% y)

Check out the below given examples to understand how it works.

Example 1

To check if a set is a subset of another set in R, use the snippet given below −

set_x1<-rpois(100,2)
set_x1

If you execute the above given snippet, it generates the following output −

[1] 5 4 2 3 1 2 2 1 3 0 1 4 2 0 4 3 2 0 1 2 2 3 1 3 2 3 1 1 2 3 1 4 0 4 2 0 0
[38] 2 1 5 2 3 1 1 1 0 0 2 1 2 1 0 2 0 0 2 0 2 1 0 2 4 5 2 1 3 3 1 3 1 1 5 3 3
[75] 5 3 3 4 3 2 3 2 1 1 0 3 1 2 3 3 0 2 1 2 3 4 3 1 2 5

To check if a set is a subset of another set in R, use the snippet given below −

set_y1<-rpois(100,5)
set_y1

If you execute the above given snippet, it generates the following output −

[1] 3 3 2 5 6 11 8 6 8 6 4 5 9 4 4 3 5 5 6 6 7 4 7 5 12
[26] 4 2 8 6 9 8 6 5 6 2 7 5 5 13 3 3 5 5 6 5 4 7 2 7 8
[51] 9 5 9 1 6 7 4 4 5 3 7 8 4 6 4 6 6 5 4 5 4 2 4 5 6
[76] 5 4 7 3 5 6 8 4 9 5 8 2 2 7 6 5 1 3 5 4 9 15 12 6 6

To check if a set is a subset of another set in R, add the following code to the above snippet −

all(set_x1 %in% set_y1)


Output

If you execute all the above given codes as a single program, it generates the following output −

[1] FALSE

Example 2

To check if a set is a subset of another set in R, use the snippet given below −

set_x2<-sample(0:9,200,replace=TRUE)
set_x2

If you execute the above given snippet, it generates the following output −

[1] 2 5 5 9 9 4 1 3 8 0 1 8 5 9 4 5 5 1 4 7 9 1 0 6 3 6 7 3 9 7 2 6 3 7 3 5 7
[38] 3 9 3 2 3 0 8 9 9 6 1 4 2 4 3 3 2 7 4 2 3 3 0 5 5 6 7 9 7 0 7 1 5 4 3 9 6
[75] 4 2 5 1 8 4 6 2 9 4 0 2 8 9 8 3 0 4 1 2 0 2 3 1 8 1 8 3 2 5 3 0 1 0 8 3 9
[112] 6 2 1 3 8 4 7 2 9 1 6 6 3 7 2 8 4 2 5 5 2 6 6 2 1 2 6 6 7 0 0 9 5 4 1 7 2
[149] 2 4 8 4 5 8 8 5 5 4 2 8 5 3 9 9 0 2 0 5 5 8 5 9 0 5 1 9 9 5 3 9 4 2 5 5 0
[186] 8 2 9 8 7 1 3 0 3 2 9 0 5 9 0

To check if a set is a subset of another set in R, use the snippet given below −

set_y2<-sample(0:9,200,replace=TRUE)
set_y2

If you execute the above given snippet, it generates the following output −

[1] 9 7 6 9 4 5 0 5 4 5 9 3 7 4 5 8 7 9 5 4 4 2 1 8 6 6 0 7 8 2 7 4 2 4 6 5 3
[38] 9 4 7 8 2 1 7 1 3 8 3 6 8 7 0 3 6 5 1 5 1 3 0 8 3 8 9 3 5 0 2 3 9 8 5 1 0
[75] 3 2 0 1 8 4 7 8 2 4 7 1 8 7 7 8 6 4 7 5 9 8 5 9 3 9 8 2 8 3 1 1 3 7 4 3 7
[112] 2 3 7 1 7 9 7 8 9 0 1 3 5 5 3 9 9 8 6 5 1 6 0 7 0 4 7 4 2 3 0 2 8 6 1 5 7
[149] 6 7 6 1 2 8 7 6 9 0 7 2 7 2 7 0 2 2 8 9 0 2 3 4 1 4 4 9 8 1 6 6 4 2 3 3 1
[186] 6 2 4 5 4 0 8 5 3 8 7 3 4 5 2

To check if a set is a subset of another set in R, add the following code to the above snippet −

all(set_x2 %in% set_y2)


Output

If you execute all the above given codes as a single program, it generates the following output −

[1] TRUE

Example 3

To check if a set is a subset of another set in R, use the snippet given below −

set_x3<-sample(1:10,200,replace=TRUE)
set_x3

If you execute the above given snippet, it generates the following output −

[1] 9 8 10 6 10 7 1 2 5 10 4 10 4 7 7 3 2 5 2 9 5 7 10 5 3
[26] 2 6 10 2 6 7 1 9 5 6 10 1 10 8 3 2 6 4 7 6 5 10 4 9 2
[51] 9 9 7 6 4 3 9 7 2 6 3 5 2 5 1 5 5 2 7 10 9 6 4 9 5
[76] 9 3 8 5 2 3 10 1 5 7 7 9 6 9 4 2 8 8 4 8 5 7 10 4 6
[101] 8 9 9 8 9 5 5 6 6 9 3 8 6 8 5 4 1 3 5 1 5 7 9 6 3
[126] 5 5 2 1 5 10 5 5 4 8 10 10 7 5 7 5 6 6 10 1 6 1 6 3 4
[151] 5 1 9 4 3 10 4 9 10 1 10 4 2 2 4 7 9 8 2 8 7 8 4 5 7
[176] 3 9 2 5 3 5 5 8 5 4 2 2 2 4 6 1 3 1 10 9 6 3 4 4 6

To check if a set is a subset of another set in R, use the snippet given below −

set_y3<-sample(1:15,200,replace=TRUE)
set_y3

If you execute the above given snippet, it generates the following output −

[1] 3 8 12 15 7 12 7 8 12 3 12 1 1 13 1 3 2 9 8 7 2 8 11 9 4
[26] 3 9 10 14 1 13 1 2 12 8 13 12 7 11 14 2 14 13 8 8 13 3 15 13 10
[51] 2 6 8 7 4 11 10 11 12 8 2 13 10 9 10 5 8 5 5 7 6 6 4 2 6
[76] 4 2 7 10 1 1 5 13 10 3 4 2 8 10 14 7 9 9 1 10 6 12 5 10 2
[101] 9 10 1 15 9 4 2 5 13 15 5 9 7 4 12 13 3 1 7 6 13 8 10 5 12
[126] 5 14 7 11 8 10 15 11 11 5 11 9 11 11 6 1 7 5 5 1 2 10 7 3 3
[151] 6 3 14 3 10 9 6 3 1 9 4 14 3 12 10 7 5 9 1 8 11 11 8 14 12
[176] 6 10 10 14 3 14 8 8 7 15 1 9 1 2 4 3 12 6 15 14 10 12 9 15 15

To check if a set is a subset of another set in R, add the following code to the above snippet −

all(set_y3 %in% set_x3)


Output

If you execute all the above given codes as a single program, it generates the following output −

[1] FALSE

Example 4

To check if a set is a subset of another set in R, use the snippet given below −

set_x4<-rpois(200,5)
set_x4

If you execute the above given snippet, it generates the following output −

[1] 7 1 6 4 6 7 4 5 5 5 7 6 4 7 7 7 5 8 3 8 10 9 2 6 5
[26] 3 4 2 3 4 5 2 4 5 5 6 1 6 5 5 6 1 5 10 3 7 5 2 5 7
[51] 0 3 4 5 5 9 5 2 2 7 3 5 2 3 7 7 4 8 3 1 5 5 9 4 3
[76] 7 11 3 1 3 4 7 7 5 11 7 8 6 4 7 4 4 5 7 6 3 3 2 7 3
[101] 8 4 2 5 3 7 4 4 7 6 4 3 5 6 4 4 3 3 4 2 11 7 2 3 5
[126] 5 9 0 5 5 3 4 5 7 6 5 7 6 6 4 6 2 4 4 2 4 5 6 3 7
[151] 7 7 6 4 10 3 3 4 9 8 9 6 6 5 3 1 5 6 4 6 3 2 4 1 7
[176] 4 5 8 8 10 5 3 7 2 1 9 5 8 4 1 5 2 9 6 3 5 5 5 8 5

To check if a set is a subset of another set in R, use the snippet given below −

set_y4<-rpois(200,10)
set_y4

If you execute the above given snippet, it generates the following output −

[1] 10 13 7 13 8 10 7 6 13 11 9 5 8 8 13 9 12 10 6 16 15 10 8 13 11
[26] 9 9 7 19 13 8 7 19 2 12 14 7 9 12 14 6 9 5 11 11 11 7 16 12 13
[51] 15 11 8 11 8 11 11 9 12 10 6 10 8 9 9 6 10 7 4 7 4 11 7 12 11
[76] 15 11 10 9 14 7 12 11 10 13 11 8 17 9 13 14 5 11 6 8 11 8 9 7 11
[101] 14 7 9 11 14 13 19 8 7 4 10 12 10 9 11 9 7 8 6 10 8 12 6 12 8
[126] 8 5 14 10 8 15 12 8 11 13 6 5 18 9 14 8 13 10 6 14 16 7 9 6 9
[151] 16 15 9 12 18 12 9 16 9 10 6 9 9 10 12 11 7 8 12 8 12 6 9 8 10
[176] 4 7 15 6 7 9 6 7 8 11 10 10 6 7 13 11 8 4 14 16 15 13 13 9 7

To check if a set is a subset of another set in R, add the following code to the above snippet −

all(set_x4 %in% set_y4)


Output

If you execute all the above given codes as a single program, it generates the following output −

[1] FALSE
Updated on 09-Nov-2021 06:43:12