r - R 中具有可重复性的多列的简单加密
问题描述
我需要为多个列生成简单的随机键,并能够生成相同的键,并且将来可能会在不更改旧键的情况下添加更多键。一些样本数据:
data <- data.frame(
tree = c(222222, 123456, 9123456),
fruit = c(12345, 55555, 123456),
seed = c(777777, 888888, 999999),
nutrients = c(12345678, 23456789, 912345678),
flowers = c(2, 3, 3)
)
我尝试了以下方法:
library(dplyr)
library(stringi)
set.seed(2)
data_ids <- data
arrange(tree, fruit, seed, nutrients) %>%
rowwise() %>%
mutate(tree_id = paste0(stri_rand_strings(1, 3, "[A-Z]"),
stri_rand_strings(1, 3, "[0-9]"),
stri_rand_strings(1, 3, "[A-Z]")),
fruit_id = paste0(stri_rand_strings(1,4, "[A-Z]"),
stri_rand_strings(1, 4, "[0-9]")),
seed_id = stri_rand_strings(1, 10, "[A-Z]"),
enc_id = stri_rand_strings(1, 10, "[0-9]"))
这种方法的问题是每个 id 都没有绑定到它的原始。树应该绑定到 tree_id 等等。这种方法非常适合一次性使用,但如果我想添加一条新记录,我不确定已经生成的 id 是否会保持不变。
提前致谢!
解决方案
我不确定我是否正确理解了您要执行的操作。我的猜测是您想以可靠的方式加密列的值。我们可以将散列算法与 {digest} 之类的包一起使用。但是我们也可以使用你的方法,我们只需要确保每个值总是转换为相同的字符串,所以如果你愿意的话,实际上它会是“不是那么随机的字符串”。
我们可以通过将现有函数包装到包装器中并set.seed
在每次调用之前调用stri_rand_strings
其中seed
每个单元格的值来做到这一点。
我了解大多数映射,但我不得不猜测enc_id
我刚刚将其映射到的绑定nutrients
,但我们可以轻松更改它。
此外,tree_id
包含两个相同的调用,stri_rand_strings(tree, 1, 3, "[A-Z]")
并且使用下面的方法,这将在每个的开头和结尾产生相同的字符串模式tree_id
。可能建议更改最后一次调用的模式。
除此之外,下面的方法会解决您的问题吗?
library(dplyr)
library(stringi)
# the data
data <- data.frame(
tree = c(222222, 123456, 9123456),
fruit = c(12345, 55555, 123456),
seed = c(777777, 888888, 999999),
nutrients = c(12345678, 23456789, 912345678),
flowers = c(2, 3, 3)
)
# lets append a new row to test the approach
data2 <- data %>%
add_row(tree = 324325,
fruit = 871556,
seed = 1111111,
nutrients = 61551548,
flowers = 4)
# custom function which sets seed before calling stri_rand_strings
seed_rand_string <- function(.seed, .n, .length, .pattern) {
set.seed(.seed)
stri_rand_strings(.n, .length, .pattern)
}
data %>%
arrange(tree, fruit, seed, nutrients) %>%
rowwise() %>%
mutate(tree_id = paste0(seed_rand_string(tree, 1, 3, "[A-Z]"),
seed_rand_string(tree, 1, 3, "[0-9]"),
seed_rand_string(tree, 1, 3, "[A-Z]")),
fruit_id = paste0(seed_rand_string(fruit, 1, 4, "[A-Z]"),
seed_rand_string(fruit, 1, 4, "[0-9]")),
seed_id = seed_rand_string(seed, 1, 10, "[A-Z]"),
enc_id = seed_rand_string(nutrients, 1, 10, "[0-9]"))
#> # A tibble: 3 x 9
#> # Rowwise:
#> tree fruit seed nutrients flowers tree_id fruit_id seed_id enc_id
#> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
#> 1 123456 55555 888888 23456789 3 UTK773UTK YRIY9639 PUGELDRY… 33355323…
#> 2 222222 12345 777777 12345678 2 KSU468KSU SWTX7878 FVTGKDXV… 82980539…
#> 3 9123456 123456 999999 912345678 3 BHU028BHU UTKI7733 WVGMPGKD… 10060645…
data2 %>%
arrange(tree, fruit, seed, nutrients) %>%
rowwise() %>%
mutate(tree_id = paste0(seed_rand_string(tree, 1, 3, "[A-Z]"),
seed_rand_string(tree, 1, 3, "[0-9]"),
seed_rand_string(tree, 1, 3, "[A-Z]")),
fruit_id = paste0(seed_rand_string(fruit, 1, 4, "[A-Z]"),
seed_rand_string(fruit, 1, 4, "[0-9]")),
seed_id = seed_rand_string(seed, 1, 10, "[A-Z]"),
enc_id = seed_rand_string(nutrients, 1, 10, "[0-9]"))
#> # A tibble: 4 x 9
#> # Rowwise:
#> tree fruit seed nutrients flowers tree_id fruit_id seed_id enc_id
#> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
#> 1 123456 55555 888888 23456789 3 UTK773UTK YRIY9639 PUGELDRY… 3335532…
#> 2 222222 12345 777777 12345678 2 KSU468KSU SWTX7878 FVTGKDXV… 8298053…
#> 3 324325 871556 1111111 61551548 4 MQW468MQW AUYE0891 MYFXDMTO… 3068266…
#> 4 9123456 123456 999999 912345678 3 BHU028BHU UTKI7733 WVGMPGKD… 1006064…
由reprex 包于 2021-02-23 创建(v0.3.0)
推荐阅读
- algorithm - 面试问题 - 哪些数字在间隔列表中出现次数最多
- r - 为什么 R 文档没有指定所需的包?
- typescript - 如何实现具有多个未命名函数成员的接口?
- c# - 通过 Telnet 连接到 Serial->Ethernet 转换器盒,得到奇怪的响应(无论我发送什么)
- c# - MVC 核心中的 NServiceBus - 使用依赖注入发送
- c++ - RPN 计算器/后修复计算器
- swift - 这个函数参数是干什么用的?
- azure - Azure Function Scale out factor 并使用消费计划上的计时器触发器进行预热
- docusignapi - 有没有办法在我的网页中嵌入 DocuSign 准备或发送信封控制面板?
- c - 当线程在 c 中调用 longjmp() 时会发生什么