---
title: "Helpers for working with vectors"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Helpers for working with vectors}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library("oeli")
```

## Generate vector subsets

Say we want to generate all subsets of a vector:

```{r, subset type 1}
v <- 1:4
subsets(v)
```

It is also possible to only generate the subsets of, say, size 2 and 3:

```{r, subset type 2}
v <- 1:4
subsets(v, n = 2:3)
```

## Split vector into chunks

Say we want to split a vector into three chunks of equal size:

```{r, chunk vectors type 1}
x <- 1:6
chunk_vector(x, n = 3)
```

Alternatively, we can split `x` into chunks of size `n = 3` by setting `type = 2`:

```{r, chunk vectors type 2}
chunk_vector(x, n = 3, type = 2)
```

Both somehow also works if `n` is not a multiple of `length(x)`:

```{r, chunk vectors uneven}
x <- 1:7
chunk_vector(x, n = 3)
chunk_vector(x, n = 3, type = 2)
```

To prevent such "odd" cases, set `strict = TRUE`:

```{r, chunk vectors strict}
try(chunk_vector(1:7, n = 3, strict = TRUE))
```

## Find the indices of first or last occurrence of unique vector elements

Say we want to find the positions of first or last occurrence of the unique elements of the following vector:

```{r, vector occurrence}
x <- c(1, 1, 1, 2, 2, 2, 3, 3, 3)
vector_occurrence(x, "first")
vector_occurrence(x, "last")
```

The returned positions correspond to 

```{r, vector occurrence unique}
unique(x)
```




