---
title: "tools"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{tools}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r setup}
library(thinkr)
```


## replace_pattern(): Replace all occurencies of a `pattern` by `replacement`

Scan a dataframe and return it with all occurencies of a `pattern` changed to `replacement`, keeping the same format.

```{r}
dataset <- data.frame(
  col_a = as.factor(letters)[1:7], 
  col_b = letters[1:7],
  col_c = 1:7,
  col_d = paste0(letters[1:7], letters[1:7]),
  stringsAsFactors = FALSE) 

# Show original dataset
dataset

# replace pattern
replace_pattern(dataset, "a", 'XXX-')
```

Exact matching with argument `exact`

```{r}
replace_pattern(dataset, "a", 'XXX-', exact = TRUE)
```

## is_likert(): Verify levels of a factor vector

Test that the levels of a factor `vec` are all to be found in the character vector `lev`. 

```{r, error=TRUE, message=TRUE}
## returns TRUE because all levels of iris$species are in c("setosa", "versicolor", "virginica")
is_likert(iris$Species, c("setosa", "versicolor", "virginica"))

## returns TRUE because all levels of iris$species are in c("setosa", "versicolor", "virginica", "banana"), even though there is actually no level "banana"
# A message is printed
is_likert(iris$Species, c("setosa", "versicolor", "virginica", "banana"))

## returns FALSE because the "virginica" level of iris$species is missing
is_likert(iris$Species, c("setosa", "versicolor"))

## returns an error
is_likert(iris$Species, c(1, 2))

## returns no error as the numeric is coerced to a character.
is_likert(iris$Species, c("setosa", 2))

```

> Warnings: is-likert does not test whether the levels of `vec` are a likert scale as in psychometry. See <https://en.wikipedia.org/wiki/Likert_scale> for example.
