An open API service indexing awesome lists of open source software.

https://github.com/teebusch/noah

An R package for generating pseudonyms that are delightful and easy to remember. It creates adorable anonymous animals like the Likeable Leech and the Proud Chikadee.
https://github.com/teebusch/noah

package pseudonymisation r rstats

Last synced: about 1 year ago
JSON representation

An R package for generating pseudonyms that are delightful and easy to remember. It creates adorable anonymous animals like the Likeable Leech and the Proud Chikadee.

Awesome Lists containing this project

README

          

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

```{r include=FALSE}
set.seed(122020)
```

# noah

[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing) [![CRAN status](https://www.r-pkg.org/badges/version/noah)](https://CRAN.R-project.org/package=noah) [![R build status](https://github.com/Teebusch/noah/workflows/R-CMD-check/badge.svg)](https://github.com/Teebusch/noah/actions) [![Codecov test coverage](https://codecov.io/gh/Teebusch/noah/branch/master/graph/badge.svg)](https://codecov.io/gh/Teebusch/noah?branch=master)

noah (*no animals were harmed*) generates pseudonyms that are delightful and easy to remember. It creates adorable anonymous animals like the *Likable Leech* and the *Proud Chickadee*.

## Installation

Install from CRAN with:

```{r eval=FALSE}
install.packages("noah")
```

Or install the development version from [Github](https://github.com/teebusch/noah) with:

```{r eval=FALSE}
# install.packages("remotes")
remotes::install_github("teebusch/noah")
```

## Usage

### Generate pseudonyms

Use `pseudonymize()` to generate a unique pseudonym for every unique element / row in a vector or data frame. `pseudonymize()` accepts multiple vectors and data frames as arguments, and will pseudonymize them row by row.

```{r}
library(noah)

pseudonymize(1:9)

pseudonymize(
c("🐰", "🐰", "🐰"),
c("🥕", "🥕", "🍰")
)
```

For extra delight, we can ask noah to generate only alliterations:

```{r}
pseudonymize(1:9, .alliterate = TRUE)
```

### Add pseudonyms to a data frame

You can use `pseudonymize()` with `dplyr::mutate()` to add a column with pseudonyms to a data frame. In this example we use the diabetic retinopathy dataset from the package `survival` and add a new column with a pseudonym for each unique id. We also use `dplyr::relocate()` to move the pseudonyms to the first column:

```{r warning=FALSE, message=FALSE}
library(dplyr)
diabetic <- as_tibble(survival::diabetic)

diabetic %>%
mutate(pseudonym = pseudonymize(id)) %>%
relocate(pseudonym)
```

For your convenience, noah also provides `add_pseudonyms()`, which wraps `mutate()` and `relocate()` and supports [tidyselect](https://tidyselect.r-lib.org/reference/language.html) syntax for selecting the key columns:

```{r}
diabetic %>%
add_pseudonyms(id, where(is.factor))
```

### Keeping track of pseudonyms with an Ark

To make sure that all pseudonyms are unique and consistent, `pseudonymize()` and `add_pseudonyms()` use an object of class `Ark` (a pseudonym archive). By default, a new `Ark` is created for each function call, but you can also provide an `Ark` yourself. This allows you to keep track of the pseudonyms that have been used and make sure that the same keys always get assigned the same pseudonym:

```{r example-ark}
ark <- Ark$new()

# split dataset into left and right eye and pseudonymize separately
diabetic_left <- diabetic %>%
filter(eye == "left") %>%
add_pseudonyms(id, .ark = ark)

diabetic_right <- diabetic %>%
filter(eye == "right") %>%
add_pseudonyms(id, .ark = ark)

# reunite the data sets again
bind_rows(diabetic_left, diabetic_right) %>%
arrange(id)
```

The ark now contains `r length(ark)` pseudonyms -- as many as there are unique id's in the dataset.

```{r}
length(unique(diabetic$id))
length(ark)
```

### Customizing an Ark

Building your own Ark allows you to customize the name parts that are used to create pseudonyms (by default, adjectives and animals). It also allow you to use names with more than two parts:

```{r}
ark <- Ark$new(parts = list(
c("Charles", "Louis", "Henry", "George"),
c("I", "II", "III", "IV"),
c("The Good", "The Wise", "The Brave", "The Mad", "The Beloved")
))

pseudonymize(1:8, .ark = ark)
```

You can also configure an `Ark` so that it generates only alliterations. Note that this behavior can still be overridden temporarily by using `.alliterate = FALSE` when you call `pseudonymize()`.

```{r}
ark <- Ark$new(alliterate = TRUE)

pseudonymize(1:12, .ark = ark)
```

## Gotchas

Noah will treat numerically identical whole numbers of type `double` and `integer` as different and give them different pseudonyms. This can cause some unexpected behavior. Consider this example:

```{r echo=TRUE, results='hide', message=FALSE}
ark <- Ark$new()

pseudonymize(1:2, .ark = ark) # creates a vector of integers c(1L, 2L)
pseudonymize(1, .ark = ark) # creates a double
```

You might expect to get 2 different pseudonyms, because in the second `pseudonymize()` you are requesting a pseudonym for the number `1`, which is already in the Ark. Instead you get three pseudonyms:

```{r}
length(ark)
```

Noah will warn you when it thinks you are making this mistake, but it might not catch it all the time. A workaround is to coerce types explicitly, for example by using `as.double()`, `as.integer()`, or `1L` to create integers.

## Related R packages

There are multiple R packages that generate fake data, including fake names, phone numbers, addresses, credit card numbers, gene sequences and more:

- [`charlatan`](https://docs.ropensci.org/charlatan/)
- [`randomNames`](https://centerforassessment.github.io/randomNames/)
- [`randNames`](https://github.com/karthik/randNames)
- [`generator`](https://github.com/paulhendricks/generator)

If you need watertight anonymization you should check out these packages for anonymizing personal identifiable information in data sets:

- [`sdcMicro`](http://sdctools.github.io/sdcMicro/index.html)
- [`sdcTable`](https://sdctools.github.io/sdcTable/index.html)
- [`anonymizer`](http://paulhendricks.io/anonymizer/)