Skip to contents

This function pairs elements of vectors or lists with their indices. The output is meant to be used in a for loop, and each element extracted with the ind(), val(), or val1() functions. A slightly lighter weight alternative to itertools::enumerate()

Usage

enumerateit(..., zeroIndexed = FALSE)

Arguments

...

Vectors or lists to be enumerated.

zeroIndexed

A logical indicating whether indexing should start from zero. Default is FALSE.

Value

A list of lists, where each inner list contains an index and the corresponding elements from the input vectors or lists.

See also

Examples

# Enumerate a vector
enumerateit(c("a", "b", "c"))
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#> 
#> [[1]][[2]]
#> [1] "a"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 2
#> 
#> [[2]][[2]]
#> [1] "b"
#> 
#> 
#> [[3]]
#> [[3]][[1]]
#> [1] 3
#> 
#> [[3]][[2]]
#> [1] "c"
#> 
#> 
# Enumerate a vector starting from zero
enumerateit(c("a", "b", "c"), zero_indexed = TRUE)
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#> 
#> [[1]][[2]]
#> [1] "a"
#> 
#> [[1]]$zero_indexed
#> [1] TRUE
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 2
#> 
#> [[2]][[2]]
#> [1] "b"
#> 
#> [[2]]$zero_indexed
#> [1] TRUE
#> 
#> 
#> [[3]]
#> [[3]][[1]]
#> [1] 3
#> 
#> [[3]][[2]]
#> [1] "c"
#> 
#> [[3]]$zero_indexed
#> [1] TRUE
#> 
#> 
# Enumerate two vectors
enumerateit(c(1, 2), c("x", "y"))
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#> 
#> [[1]][[2]]
#> [1] 1
#> 
#> [[1]][[3]]
#> [1] "x"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 2
#> 
#> [[2]][[2]]
#> [1] 2
#> 
#> [[2]][[3]]
#> [1] "y"
#> 
#>