-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
170 lines (116 loc) · 5.84 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{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 <img src="man/figures/logo.png" align="right" height="139"/>
<!-- badges: start -->
[![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)
<!-- badges: end -->
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/)