This function turns a "ragged" matrix into a vector. Consider a case where you have a matrix that looks like:
1, 0, 1
2, 3, NA
NA, 4, NA
Here, each row represents a series of values, where missing values are represented by NA
. This can be turned into a vector form going from left to right and top to bottom of the matrix, as in c(1, 0, 1, 2, 3, 4)
, plus a vector c(1, 4, 6)
, which provides the index of the first non-NA
value in each row of the matrix in the vector, plus another vector, c(1, 1, 1, 2, 2, 3)
, indicating the row to which each value in the vector belonged.
Value
A list with one vector per matrix, plus 1) a vector named startIndex
with indices of start values, and 2) a vector named row
with one value per non-skip
value in each matrix.
Examples
# default
x <- matrix(c(1, 0, 1, 2, 3, NA, NA, 4, NA), byrow = TRUE, nrow = 3)
unragMatrix(x)
#> $x
#> [1] 1 0 1 2 3 4
#>
#> $startIndex
#> [1] 1 4 6
#>
#> $row
#> [1] 1 1 1 2 2 3
#>
# skip nothing
unragMatrix(x, skip = NULL)
#> $x
#> [1] 1 0 1 2 3 NA NA 4 NA
#>
#> $startIndex
#> [1] 1 4 7
#>
#> $row
#> [1] 1 1 1 2 2 2 3 3 3
#>
# skips rows with all "skip" values
y <- matrix(c(1, 0, 1, NA, NA, NA, NA, 4, NA), byrow = TRUE, nrow = 3)
unragMatrix(y)
#> $x
#> [1] 1 0 1 4
#>
#> $startIndex
#> [1] 1 2 4
#>
#> $row
#> [1] 1 1 1 3
#>