## Permutation Pyramid

So I had to make a combination of different values based on elements in a vector. With a few tweaks and what not I made a little function to find all the combinations and permutations of a given vector. Enjoy

The function:

```# creat a list of combinations and
# permutations of elements from
# a single vector. This requires
# a few libraries:
# library( dplyr )
# library( purrr )
# library( combinat )

# "order.matters" is means that
# for every combination of elements
# find every order they can be
# arranged.

permutation.pyramid <- function( v, order.matters = TRUE ){

# get the unique combinations of elements
# and flatten them into a one dimensional list
out <- 1:length(v) %>%
lapply(function( x ){
combn( v, x ) %>% as.data.frame()
}) %>%
purrr::flatten() %>%
unname()

# if order.matters then find all the
# arrangements of each combination
if( order.matters ){
out <- out %>%
lapply(function( x ){
combinat::permn( x )
}) %>%
purrr::flatten()
}

# return list of permutations
return( out )
}

```

Use and examples.

Find all combinations and permutations of a given vector

```> test <- c("A","B","C")
> permutation.pyramid( test )
[]
 "A"

[]
 "B"

[]
 "C"

[]
 "A" "B"

[]
 "B" "A"

[]
 "A" "C"

[]
 "C" "A"

[]
 "B" "C"

[]
 "C" "B"

[]
 "A" "B" "C"

[]
 "A" "C" "B"

[]
 "C" "A" "B"

[]
 "C" "B" "A"

[]
 "B" "C" "A"

[]
 "B" "A" "C"
```

Find all combinations of vector

```> test <- c("A","B","C")
> permutation.pyramid( test, order.matters = FALSE )
[]
 "A"

[]
 "B"

[]
 "C"

[]
 "A" "B"

[]
 "A" "C"

[]
 "B" "C"

[]
 "A" "B" "C"
```

Anyways I hope that’ll be useful for someone!