Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Here a snippet of R to generate a Version 4 UUID. Dunno why there wouldn’t be an official function for that in the standard libraries, but if there is, I couldn’t find it.

## Version 4 UUIDs have the form:
##    xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
##    where x is any hexadecimal digit and
##    y is one of 8, 9, A, or B
##    f47ac10b-58cc-4372-a567-0e02b2c3d479
uuid <- function(uppercase=FALSE) {

hex_digits <- c(as.character(0:9), letters[1:6])
hex_digits <- if (uppercase) toupper(hex_digits) else hex_digits

y_digits <- hex_digits[9:12]

paste(
paste0(sample(hex_digits, 8), collapse=''),
paste0(sample(hex_digits, 4), collapse=''),
paste0('4', sample(hex_digits, 3), collapse=''),
paste0(sample(y_digits,1),
sample(hex_digits, 3),
collapse=''),
paste0(sample(hex_digits, 12), collapse=''),
sep='-')
}


View as a gist: https://gist.github.com/cbare/5979354