
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.
enum(x)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.
enum(letters)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#> [26] 26
enum(numeric(0))
#> integer(0)
# Compare with:
1:length(numeric(0))
#> [1] 1 0
enum(NULL)
#> integer(0)
letters5 <- letters[1:5]
for (i in enum(letters5)) cat("letter", i, "=", letters5[i], "\n")
#> letter 1 = a
#> letter 2 = b
#> letter 3 = c
#> letter 4 = d
#> letter 5 = e