gpt4 book ai didi

r - 在 R 中生成多个字符的(随机)序列

转载 作者:行者123 更新时间:2023-12-01 07:25:44 25 4
gpt4 key购买 nike

  • 我可以使用创建一系列单个字母
    LETTERS[seq( from = 1, to = 10 )]
    letters[seq( from = 1, to = 10 )]
  • 我可以创建一个不同长度的随机字符串,使用 random包裹
    library(random)
    string <- randomStrings(n=10, len=5, digits=TRUE, upperalpha=TRUE,
    loweralpha=TRUE, unique=TRUE, check=TRUE)

  • 不幸的是,我不能使用 set.seed函数例如 2 .

    有没有办法在每次运行 R 文件时创建(唯一)字符串的相同(随机)组合?

    我的结果看起来像这样(每次运行该函数时结果都相同):
           V1     
    [1,] "k7QET"
    [2,] "CLlWm"
    [3,] "yPuwh"
    [4,] "JJqEX"
    [5,] "38soF"
    [6,] "xkozk"
    [7,] "uaiOW"
    [8,] "tZcrW"
    [9,] "8K4Cc"
    [10,] "RAhuU"

    最佳答案

    在一个文件中,比如 test.R,添加以下内容

    set.seed(1)
    stringi::stri_rand_strings(10, 5)

    然后每次都可以重现。
    replicate(5, source("test.R", verbose = FALSE)$value)
    # [,1] [,2] [,3] [,4] [,5]
    # [1,] "GNZuC" "GNZuC" "GNZuC" "GNZuC" "GNZuC"
    # [2,] "twed3" "twed3" "twed3" "twed3" "twed3"
    # [3,] "CAgNl" "CAgNl" "CAgNl" "CAgNl" "CAgNl"
    # [4,] "UizNm" "UizNm" "UizNm" "UizNm" "UizNm"
    # [5,] "vDe7G" "vDe7G" "vDe7G" "vDe7G" "vDe7G"
    # [6,] "N0NrL" "N0NrL" "N0NrL" "N0NrL" "N0NrL"
    # [7,] "TbUBp" "TbUBp" "TbUBp" "TbUBp" "TbUBp"
    # [8,] "fn6iP" "fn6iP" "fn6iP" "fn6iP" "fn6iP"
    # [9,] "oemYW" "oemYW" "oemYW" "oemYW" "oemYW"
    # [10,] "m1Tjg" "m1Tjg" "m1Tjg" "m1Tjg" "m1Tjg"

    作为 source() 的替代方案,您可以使用 parse()那里。
    replicate(5, eval(parse("test.R")))

    关于r - 在 R 中生成多个字符的(随机)序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29344795/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com