项目作者: Teebusch

项目描述 :
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.
高级语言: R
项目地址: git://github.com/Teebusch/noah.git
创建时间: 2020-10-08T15:27:17Z
项目社区:https://github.com/Teebusch/noah

开源协议:Other

下载


" class="reference-link">noah

Lifecycle:
maturing
CRAN
status
R build
status
Codecov test
coverage

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:

  1. install.packages("noah")

Or install the development version from
Github with:

  1. # install.packages("remotes")
  2. 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.

  1. library(noah)
  2. pseudonymize(1:9)
  3. #> [1] "Impartial Rat" "Superficial Bird" "Royal Orca"
  4. #> [4] "Earsplitting Python" "Fascinated Donkey" "Defeated Trout"
  5. #> [7] "Encouraging Stoat" "Null Grouse" "Axiomatic Octopus"
  6. pseudonymize(
  7. c("🐰", "🐰", "🐰"),
  8. c("🥕", "🥕", "🍰")
  9. )
  10. #> [1] "Bloody Clam" "Bloody Clam" "Depressed Egret"

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

  1. pseudonymize(1:9, .alliterate = TRUE)
  2. #> [1] "Safe Sole" "Callous Clownfish" "Polite Panda"
  3. #> [4] "Best Badger" "Like Leopard" "Many Mole"
  4. #> [7] "Smiling Slug" "Sweltering Silverfish" "Sick Sloth"

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:

  1. library(dplyr)
  2. diabetic <- as_tibble(survival::diabetic)
  3. diabetic %>%
  4. mutate(pseudonym = pseudonymize(id)) %>%
  5. relocate(pseudonym)
  6. #> # A tibble: 394 x 9
  7. #> pseudonym id laser age eye trt risk time status
  8. #> <chr> <int> <fct> <int> <fct> <int> <int> <dbl> <int>
  9. #> 1 Possessive Armadillo 5 argon 28 left 0 9 46.2 0
  10. #> 2 Possessive Armadillo 5 argon 28 right 1 9 46.2 0
  11. #> 3 Crowded Vole 14 xenon 12 left 1 8 42.5 0
  12. #> 4 Crowded Vole 14 xenon 12 right 0 6 31.3 1
  13. #> 5 Productive Heron 16 xenon 9 left 1 11 42.3 0
  14. #> 6 Productive Heron 16 xenon 9 right 0 11 42.3 0
  15. #> 7 Frequent Okapi 25 xenon 9 left 0 11 20.6 0
  16. #> 8 Frequent Okapi 25 xenon 9 right 1 11 20.6 0
  17. #> 9 Giant Lobster 29 xenon 13 left 0 10 0.3 1
  18. #> 10 Giant Lobster 29 xenon 13 right 1 9 38.8 0
  19. #> # ... with 384 more rows

For your convenience, noah also provides add_pseudonyms(), which wraps
mutate() and relocate() and supports
tidyselect
syntax for selecting the key columns:

  1. diabetic %>%
  2. add_pseudonyms(id, where(is.factor))
  3. #> # A tibble: 394 x 9
  4. #> pseudonym id laser age eye trt risk time status
  5. #> <chr> <int> <fct> <int> <fct> <int> <int> <dbl> <int>
  6. #> 1 Doubtful Horse 5 argon 28 left 0 9 46.2 0
  7. #> 2 Caring Heron 5 argon 28 right 1 9 46.2 0
  8. #> 3 Grey Chicken 14 xenon 12 left 1 8 42.5 0
  9. #> 4 Giddy Vole 14 xenon 12 right 0 6 31.3 1
  10. #> 5 Overrated Caterpillar 16 xenon 9 left 1 11 42.3 0
  11. #> 6 Angry Oribi 16 xenon 9 right 0 11 42.3 0
  12. #> 7 Roasted Sawfish 25 xenon 9 left 0 11 20.6 0
  13. #> 8 Spectacular Lion 25 xenon 9 right 1 11 20.6 0
  14. #> 9 Panoramic Owl 29 xenon 13 left 0 10 0.3 1
  15. #> 10 Orange Bear 29 xenon 13 right 1 9 38.8 0
  16. #> # ... with 384 more rows

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:

  1. ark <- Ark$new()
  2. # split dataset into left and right eye and pseudonymize separately
  3. diabetic_left <- diabetic %>%
  4. filter(eye == "left") %>%
  5. add_pseudonyms(id, .ark = ark)
  6. diabetic_right <- diabetic %>%
  7. filter(eye == "right") %>%
  8. add_pseudonyms(id, .ark = ark)
  9. # reunite the data sets again
  10. bind_rows(diabetic_left, diabetic_right) %>%
  11. arrange(id)
  12. #> # A tibble: 394 x 9
  13. #> pseudonym id laser age eye trt risk time status
  14. #> <chr> <int> <fct> <int> <fct> <int> <int> <dbl> <int>
  15. #> 1 Faulty Swift 5 argon 28 left 0 9 46.2 0
  16. #> 2 Faulty Swift 5 argon 28 right 1 9 46.2 0
  17. #> 3 Tart Crab 14 xenon 12 left 1 8 42.5 0
  18. #> 4 Tart Crab 14 xenon 12 right 0 6 31.3 1
  19. #> 5 Sticky Barnacle 16 xenon 9 left 1 11 42.3 0
  20. #> 6 Sticky Barnacle 16 xenon 9 right 0 11 42.3 0
  21. #> 7 Brainy Moth 25 xenon 9 left 0 11 20.6 0
  22. #> 8 Brainy Moth 25 xenon 9 right 1 11 20.6 0
  23. #> 9 Poised Urial 29 xenon 13 left 0 10 0.3 1
  24. #> 10 Poised Urial 29 xenon 13 right 1 9 38.8 0
  25. #> # ... with 384 more rows

The ark now contains 197 pseudonyms – as many as there are unique id’s
in the dataset.

  1. length(unique(diabetic$id))
  2. #> [1] 197
  3. length(ark)
  4. #> [1] 197

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:

  1. ark <- Ark$new(parts = list(
  2. c("Charles", "Louis", "Henry", "George"),
  3. c("I", "II", "III", "IV"),
  4. c("The Good", "The Wise", "The Brave", "The Mad", "The Beloved")
  5. ))
  6. pseudonymize(1:8, .ark = ark)
  7. #> [1] "Louis IV The Brave" "George II The Good" "Louis I The Good"
  8. #> [4] "Charles IV The Wise" "Charles IV The Brave" "Louis II The Mad"
  9. #> [7] "Charles I The Brave" "George I The Beloved"

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().

  1. ark <- Ark$new(alliterate = TRUE)
  2. pseudonymize(1:12, .ark = ark)
  3. #> [1] "Hard-To-Find Hyena" "Well-Made Whippet" "Momentous Mosquito"
  4. #> [4] "Mushy Macaw" "Complete Clownfish" "Three Tahr"
  5. #> [7] "Phobic Pheasant" "Squealing Swallow" "Subdued Swan"
  6. #> [10] "Mundane Marsupial" "Complex Centipede" "Cruel Crane"

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:

  1. ark <- Ark$new()
  2. pseudonymize(1:2, .ark = ark) # creates a vector of integers c(1L, 2L)
  3. 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:

  1. length(ark)
  2. #> [1] 3

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.

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

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