What is the combn() function in R?

Overview

The combn() function in R is used to return the combination of the elements of a given argument x taken m at a time.

Syntax

combn(x, m)
Syntax for the "combn()" function

Parameter value

The combn() function takes the following parameter values:

  • x: This represents the vector source for combinations.
  • m: This represents the number of elements to choose from the x object.

Return value

The combn() function returns a list or an array.

Code example

# creating the vector source
x <- c('A', 'B', 'C', 'D', 'E')
# implementing the combn() function for diferent number of elements
combn(x, 1)
combn(x, 2)
combn(x, 3)

Code explanation

  • Line 2: We create a vector source x.
  • Lines 5–7: We implement the combn() function, using different numbers of elements to choose from the vector object x.