R 生成唯一的字母数字标识
我有一个数据框,我想在其中添加一列,其中包含不重复的字母数字值 首先,我修改了我在博客上找到的一个功能。() 当n非常接近排列数时,如果R 生成唯一的字母数字标识,r,R,我有一个数据框,我想在其中添加一列,其中包含不重复的字母数字值 首先,我修改了我在博客上找到的一个功能。() 当n非常接近排列数时,如果while必须运行很多次=>则速度非常慢 > system.time(idGenerator(62^2, 2)) utilisateur système écoulé 8.00 0.00 8.02 > system.time(idGenerator(62^3, 3)) T
while
必须运行很多次=>则速度非常慢
> system.time(idGenerator(62^2, 2))
utilisateur système écoulé
8.00 0.00 8.02
> system.time(idGenerator(62^3, 3))
Timing stopped at: 584.35 16.66 602.46
但对于长id字符串来说,这是可以接受的:
> system.time(idGenerator(250000, 12))
utilisateur système écoulé
3.2 0.0 3.2
然而,创建一个列仍然需要3sec+,所以我正在寻找一种更快的方法。
我知道循环不是很好,我应该更喜欢矢量化,但我不是真正的代码优化大师。因此,如果您有任何想法,请提前感谢。我建议您查看“stringi”软件包中的
stri\u rand\u strings
函数:
库(stringi)
stri_和rand_弦(10,3)
#[1]“wsm”“FvH”“UXm”“14t”“rvv”“Pfo”“mzK”“20b”“O9P”“ZOr”
system.time(X)您是否尝试过“stringi”软件包中的stri_rand_strings
?功能很棒,我不知道。谢谢。我刚刚添加了复制控件:idGenerator
> system.time(idGenerator(62^2, 2))
utilisateur système écoulé
8.00 0.00 8.02
> system.time(idGenerator(62^3, 3))
Timing stopped at: 584.35 16.66 602.46
> system.time(idGenerator(250000, 12))
utilisateur système écoulé
3.2 0.0 3.2
library(stringi)
stri_rand_strings(10, 3)
# [1] "wsm" "FvH" "UXm" "14t" "rvv" "Pfo" "mzK" "20b" "O9P" "ZOr"
system.time(X <- stri_rand_strings(250000, 12))
# user system elapsed
# 0.327 0.003 0.333
length(unique(X))
# [1] 250000
head(X)
# [1] "WxRPZjt0uFaI" "E129Ug0Vif3f" "qXGzQDO0LzvG"
# [4] "9D4guGMf2jZ1" "Qw1p7reH4XKg" "0gziFNnZ16p8"