How can I generate a GUID in R?

前端 未结 4 1540
情书的邮戳
情书的邮戳 2020-12-10 10:54

How can I generate GUIDs and UUIDs in R?

I would like to be able to generate GUIDs based on the hardware etc. of the machine running the rsession.

As a fall

相关标签:
4条回答
  • 2020-12-10 11:37

    The optimal choice for this now is the uuid package. It consists of one function (UUIDgenerate) that doesn't rely on R's internal random number generators and so doesn't suffer any consequences from using set.seed in a session as @thelatemail's answer does. You can choose to have the UUID be generated either by the package's internal random number generator or based on time.

    0 讨论(0)
  • 2020-12-10 11:44

    I know nothing about the intricacies of UUID's, but would something like this do?

    baseuuid <- paste(sample(c(letters[1:6],0:9),30,replace=TRUE),collapse="")
    
    paste(
        substr(baseuuid,1,8),
        "-",
        substr(baseuuid,9,12),
        "-",
        "4",
        substr(baseuuid,13,15),
        "-",
        sample(c("8","9","a","b"),1),
        substr(baseuuid,16,18),
        "-",
        substr(baseuuid,19,30),
        sep="",
        collapse=""
    )
    # result like: "f7bd11ed-fca9-42e5-8c3e-4464cd02e0fa"
    

    This should be in line with http://en.wikipedia.org/wiki/Uuid#Version_4_.28random.29

    0 讨论(0)
  • 2020-12-10 11:46

    Bioconductor had an Ruuid package that doesn't seem to be there anymore, but a google search on "Ruuid" will point to places that you can download it.

    0 讨论(0)
  • 2020-12-10 11:49

    If you are using R in Unix environment, you can get UUID in R using system() command.

    On Linux (Ubuntu 12.04 LTS):

    my_uuid <- system("uuid",intern=T)
    my_uuid
    [1] 0f62f1de-418d-11e3-8a19-cb0ceccb58ec
    

    On Mac OS X 10.8:

    my_uuid <- system("uuidgen", intern=T)
    my_uuid
    [1] 9A9D64DF-EB01-47E7-B16E-DC0343951883
    

    Far as I know, both uuid and uuidgen follows UUID Version 4 format.

    0 讨论(0)
提交回复
热议问题