Sequence along a dimension

seq_along_dim(x, which_dim)

seq_along_rows(x)

seq_along_cols(x)

Arguments

x

a dataframe, array or vector. For seq_along_rows, and seq_along_cols sequence along the first and last dimensions, respectively. Atomic vectors are treated as 1 dimensional arrays (i.e., seq_along_rows is equivalent to seq_along when x is an atomic vector or list).

which_dim

a scalar integer or character string, specifying which dimension to generate a sequence for. Negative numbers count from the back.

Value

a vector of integers 1:nrow(x), safe for use in for loops and vectorized equivalents.

Examples

for (r in seq_along_rows(mtcars[1:4,])) print(mtcars[r,])
#> mpg cyl disp hp drat wt qsec vs am gear carb #> Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4 #> mpg cyl disp hp drat wt qsec vs am gear carb #> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4 #> mpg cyl disp hp drat wt qsec vs am gear carb #> Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 #> mpg cyl disp hp drat wt qsec vs am gear carb #> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
x <- 1:3 identical(seq_along_rows(x), seq_along(x))
#> [1] TRUE