How can I generate a GUID in R?

流过昼夜 提交于 2019-11-29 05:33:46

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.

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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!