Skip to main content
R

How to Generate UUID in R

Using uuid package

How to Generate UUID in R. Call UUIDgenerate() to generate a UUID in R in one line. Looking for random IDs on iOS, macOS, watchOS and tvOS? The swift uuid page has copy-paste code.

Generate UUID in R

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

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.

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. Add the dependency to Cargo.toml, enable the right feature flags, and the rust uuid examples compile with only the code paths you need.

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(). Typed alternatives for versions 1, 5, and 7 live in an npm package that a typescript uuid project can add when random values are not enough.

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

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!