Get All Possible Subsets of Vector in R (2 Examples)
In this tutorial you’ll learn how to find all possible subsets of vector elements in the R programming language.
Table of contents:
Let’s jump right to the examples!
Example Data
The following data is used as basement for this R programming tutorial:
my_vec <- paste0("C", 1:3) # Create example vector my_vec # Print example vector # [1] "C1" "C2" "C3"
Have a look at the previous RStudio console output. It shows that our example data is a vector containing the three vector elements “C1”, “C2”, and “C3”.
Example 1: Return All Possible Combinations of Vector Using combinat Package
Example 1 demonstrates how to use the combinat package to get all possible subsets of a vector.
To be able to use the functions of the combinat package, we first have to install and load combinat:
install.packages("combinat") # Install combinat package library("combinat") # Load combinat
Next, we can use the unlist, lapply, length, and combn functions to create a list containing all subset combinations of our vector:
my_combi1 <- unlist(lapply(1:length(my_vec), # Get all combinations combinat::combn, x = my_vec, simplify = FALSE), recursive = FALSE) my_combi1 # Print all combinations # [[1]] # [1] "C1" # # [[2]] # [1] "C2" # # [[3]] # [1] "C3" # # [[4]] # [1] "C1" "C2" # # [[5]] # [1] "C1" "C3" # # [[6]] # [1] "C2" "C3" # # [[7]] # [1] "C1" "C2" "C3"
The previous output of the RStudio console shows our result: We have created a list where each list element represents a different subset of our input vector.
Example 2: Return All Possible Combinations of Vector Using sets Package
Alternatively to the combinat package, we can also use the sets package to get all possible subsets of a vector.
First, we need to install and load the sets package:
install.packages("sets") # Install sets package library("sets") # Load sets package
In the next step, we can apply the set_power function to return all subsets of our vector:
my_combi2 <- set_power(my_vec) # Get all combinations my_combi2 # Print all combinations # {{}, {"C1"}, {"C2"}, {"C3"}, {"C1", "C2"}, {"C1", "C3"}, {"C2", "C3"}, {"C1", "C2", "C3"}}
Note that the previous R code has created a set object. You can learn more about the handling of sets here.
Video, Further Resources & Summary
If you need further info on the topics of this page, I recommend watching the following video on my YouTube channel. I explain the R programming codes of this page in the video:
In addition, you may want to read some of the other tutorials on my website:
- Calculate Combinations & Permutations in R
- Get Frequency of Elements with Certain Value in Vector (2 R Examples)
- Get Last Value of Vector
- Test for Equality of All Vector Elements
- Get All Factor Levels of Vector & Data Frame Column
- All R Programming Examples
In this article you have learned how to identify all possible subsets of elements in a vector in R programming. Don’t hesitate to tell me about it in the comments section, if you have additional questions.