Iterate over an array or data frame by a specified dimension.
Source:R/iteror_array.R
, R/iteror_data_frame.R
iteror.array.Rd
Iterate over an array or data frame by a specified dimension.
Usage
# S3 method for array
iteror(
obj,
...,
by = c("cell", "row", "column"),
chunkSize,
chunks,
recycle = FALSE,
drop = FALSE,
rowMajor = TRUE
)
# S3 method for matrix
iteror(
obj,
...,
by = c("cell", "row", "column"),
chunkSize,
chunks,
recycle = FALSE,
drop = FALSE,
rowMajor = TRUE
)
# S3 method for data.frame
iteror(obj, ..., recycle = FALSE, chunkSize, chunks, by = c("column", "row"))
Arguments
- obj
An object to iterate over.
- ...
Undocumented.
- by
Which dimension to slice an array or data frame by. Can be "cell", "row", "column", or numeric dimensions.
- chunkSize
The thickness of the slice to take along the specified dimension.
- chunks
How many slices to take.
- recycle
If TRUE, the iteror starts over on reaching the end.
- drop
Whether to drop the array dimensions enumerated over.
- rowMajor
If TRUE, will return slices in order with the first indices varying fastest (same as in i_enumerate).
Examples
l <- iteror(letters, chunkSize=7)
as.list(l)
#> [[1]]
#> [1] "a" "b" "c" "d" "e" "f" "g"
#>
#> [[2]]
#> [1] "h" "i" "j" "k" "l" "m" "n"
#>
#> [[3]]
#> [1] "o" "p" "q" "r" "s" "t" "u"
#>
#> [[4]]
#> [1] "v" "w" "x" "y" "z"
#>
a <- array(1:8, c(2, 2, 2))
# iterate over all the slices
it <- iteror(a, by=3)
as.list(it)
#> [[1]]
#> , , 1
#>
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#>
#> [[2]]
#> , , 1
#>
#> [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
#>
# iterate over all the columns of each slice
it <- iteror(a, by=c(2, 3))
as.list(it)
#> [[1]]
#> , , 1
#>
#> [,1]
#> [1,] 1
#> [2,] 2
#>
#>
#> [[2]]
#> , , 1
#>
#> [,1]
#> [1,] 3
#> [2,] 4
#>
#>
#> [[3]]
#> , , 1
#>
#> [,1]
#> [1,] 5
#> [2,] 6
#>
#>
#> [[4]]
#> , , 1
#>
#> [,1]
#> [1,] 7
#> [2,] 8
#>
#>
# iterate over all the rows of each slice
it <- iteror(a, by=c(1, 3))
as.list(it)
#> [[1]]
#> , , 1
#>
#> [,1] [,2]
#> [1,] 1 3
#>
#>
#> [[2]]
#> , , 1
#>
#> [,1] [,2]
#> [1,] 2 4
#>
#>
#> [[3]]
#> , , 1
#>
#> [,1] [,2]
#> [1,] 5 7
#>
#>
#> [[4]]
#> , , 1
#>
#> [,1] [,2]
#> [1,] 6 8
#>
#>