Skip to main content
R

How to Generate UUID in R

Using uuid package

You won't find UUID generation in R's base language — for that you need the uuid package from CRAN, which gives you a one-line UUIDgenerate() call that returns a random, RFC 4122-compliant character string ready to drop straight into a data frame or database write. That matters any time you're merging datasets, tagging experimental observations, or writing rows to a table where an accidental ID collision would silently corrupt your results. Below you'll find copy-paste ready code for generating a UUID in R, checking whether a string looks like a valid UUID, and the package's time-based alternative to the default random format. The bash uuid guide covers uuidgen, /proc/sys/kernel/random/uuid, and Python one-liners for shell scripting.

Generate UUID in R

UUIDgenerate()
550e8400-e29b-41d4-a716-446655440000

How to Generate a UUID in R

The standard way to generate a UUID in R is the uuid package from CRAN — base R has no built-in UUID generator, so install the package once, then call UUIDgenerate() to produce a random (v4) identifier as a plain character string. The rust uuid generator reference explains how to generate v4 and v7 UUIDs with Rust's ownership-safe uuid crate.

install.R
install.packages("uuid")
main.R
library(uuid)

my_uuid <- UUIDgenerate()
print(my_uuid)
# [1] "550e8400-e29b-41d4-a716-446655440000"

Explanation

Validate a UUID String in R

The uuid package has no dedicated parser, so the simplest way to check whether a string is a valid UUID in R is a regular expression match against the canonical 8-4-4-4-12 hex pattern using grepl(). The c++ uuid reference explains how to generate and parse UUIDs in C++ with proper formatting.

main.R
my_uuid <- UUIDgenerate()

is_valid <- grepl(
  "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
  my_uuid,
  ignore.case = TRUE
)

print(is_valid)
# [1] TRUE

Explanation

Other Options for Generating UUIDs in R

UUIDgenerate()'s default output covers most use cases, but the use.time argument switches to a time-based UUID, and the n argument generates several UUIDs in a single call — handy when you need to stamp every row of a data frame at once.

main.R
library(uuid)

# Time-based UUID instead of pure random
time_based <- UUIDgenerate(use.time = TRUE)

# Generate 10 UUIDs in a single call
batch_ids <- UUIDgenerate(n = 10)

Explanation

Comments & Feedback

Share your experience or ask questions about this tool

Copied!