Skip to contents

This function generates subsets of a vector.

Usage

subsets(v, n = seq_along(v))

Arguments

v

[atomic()`]
A vector of elements.

n

[integer(1)`]
The requested subset sizes.

Value

A list, each element is a subset of v.

Examples

v <- 1:3
subsets(v)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 1 2
#> 
#> [[5]]
#> [1] 1 3
#> 
#> [[6]]
#> [1] 2 3
#> 
#> [[7]]
#> [1] 1 2 3
#> 
subsets(v, c(1, 3)) # only subsets of length 1 or 3
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 1 2 3
#> 
subsets(integer())  # trivial case works
#> list()