enum: Enumerate items in an object

View source: R/misc.R

enumR Documentation

Enumerate items in an object

Description

enum() creates a vector of integers from 1 to length of the object (it enumerates items in the object), except if the object is empty. It is particularly useful in the ⁠for(i in enum(object))⁠ construct.

Usage

enum(x)

Arguments

x

Any object.

Note

The pattern ⁠for(i in 1:length(object))⁠ is often found, but it fails in case length(object) == 0! enum() is indeed a synonym of seq_along(), but the later one is less expressive in the context.

See Also

seq_along()

Examples

enum(letters)
enum(numeric(0))
# Compare with:
1:length(numeric(0))
enum(NULL)
letters5 <- letters[1:5]
for (i in enum(letters5)) cat("letter", i, "=", letters5[i], "\n")

SciViews/SciViews documentation built on Sept. 16, 2023, 10:26 p.m.