Standardize a Sparse Matrix in Triplet Format
asUniqueT.RdDetect or standardize a TsparseMatrix with
unsorted or duplicated \((i,j)\) pairs.
Usage
anyDuplicatedT(x, ...)
isUniqueT(x, byrow = FALSE, isT = is(x, "TsparseMatrix"))
asUniqueT(x, byrow = FALSE, isT = is(x, "TsparseMatrix"))
aggregateT(x)Arguments
- x
an R object.
anyDuplicatedTandaggregateTrequirexinheriting fromTsparseMatrix.asUniqueTrequiresxinheriting fromMatrixand coercesxtoTsparseMatrixif necessary.- ...
optional arguments passed to the default method for generic function
anyDuplicated.- byrow
a logical indicating if
xshould be sorted by row then by column.- isT
a logical indicating if
xinherits from virtual classTsparseMatrix.
Value
anyDuplicatedT(x) returns the index of the first duplicated
\((i,j)\) pair in x (0 if there are no duplicated pairs).
isUniqueT(x) returns TRUE if x is a
TsparseMatrix with sorted, nonduplicated
\((i,j)\) pairs and FALSE otherwise.
asUniqueT(x) returns the unique
TsparseMatrix representation of x with
sorted, nonduplicated \((i,j)\) pairs. Values corresponding to
identical \((i,j)\) pairs are aggregated by addition, where in the
logical case “addition” refers to logical OR.
aggregateT(x) aggregates without sorting.
See also
Virtual class TsparseMatrix.
Examples
example("dgTMatrix-class", echo=FALSE)
#> Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
#> ..@ i : int [1:9] 0 1 3 0 1 3 2 3 2
#> ..@ j : int [1:9] 0 0 0 2 2 2 4 5 6
#> ..@ Dim : int [1:2] 4 7
#> ..@ Dimnames:List of 2
#> .. ..$ : NULL
#> .. ..$ : NULL
#> ..@ x : num [1:9] 1 2 4 9 10 12 19 24 27
#> ..@ factors : list()
#> Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
#> ..@ i : int [1:5] 1 1 0 3 3
#> ..@ j : int [1:5] 2 2 4 0 0
#> ..@ Dim : int [1:2] 4 5
#> ..@ Dimnames:List of 2
#> .. ..$ : NULL
#> .. ..$ : NULL
#> ..@ x : num [1:5] 10 20 30 40 50
#> ..@ factors : list()
## -> 'T2' with (i,j,x) slots of length 5 each
T2u <- asUniqueT(T2)
stopifnot(## They "are" the same (and print the same):
all.equal(T2, T2u, tol=0),
## but not internally:
anyDuplicatedT(T2) == 2,
anyDuplicatedT(T2u) == 0,
length(T2 @x) == 5,
length(T2u@x) == 3)
isUniqueT(T2 ) # FALSE
#> [1] FALSE
isUniqueT(T2u) # TRUE
#> [1] TRUE
T3 <- T2u
T3[1, c(1,3)] <- 10; T3[2, c(1,5)] <- 20
T3u <- asUniqueT(T3)
str(T3u) # sorted in 'j', and within j, sorted in i
#> Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
#> ..@ i : int [1:7] 0 1 3 0 1 0 1
#> ..@ j : int [1:7] 0 0 0 2 2 4 4
#> ..@ Dim : int [1:2] 4 5
#> ..@ Dimnames:List of 2
#> .. ..$ : NULL
#> .. ..$ : NULL
#> ..@ x : num [1:7] 10 20 90 10 30 30 20
#> ..@ factors : list()
stopifnot(isUniqueT(T3u))
## Logical l.TMatrix and n.TMatrix :
(L2 <- T2 > 0)
#> 4 x 5 sparse Matrix of class "lgTMatrix"
#>
#> [1,] . . . . |
#> [2,] . . | . .
#> [3,] . . . . .
#> [4,] | . . . .
validObject(L2u <- asUniqueT(L2))
#> [1] TRUE
(N2 <- as(L2, "nMatrix"))
#> 4 x 5 sparse Matrix of class "ngTMatrix"
#>
#> [1,] . . . . |
#> [2,] . . | . .
#> [3,] . . . . .
#> [4,] | . . . .
validObject(N2u <- asUniqueT(N2))
#> [1] TRUE
stopifnot(N2u@i == L2u@i, L2u@i == T2u@i, N2@i == L2@i, L2@i == T2@i,
N2u@j == L2u@j, L2u@j == T2u@j, N2@j == L2@j, L2@j == T2@j)
# now with a nasty NA [partly failed in Matrix 1.1-5]:
L.0N <- L.1N <- L2
L.0N@x[1:2] <- c(FALSE, NA)
L.1N@x[1:2] <- c(TRUE, NA)
validObject(L.0N)
#> [1] TRUE
validObject(L.1N)
#> [1] TRUE
(m.0N <- as.matrix(L.0N))
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] FALSE FALSE FALSE FALSE TRUE
#> [2,] FALSE FALSE NA FALSE FALSE
#> [3,] FALSE FALSE FALSE FALSE FALSE
#> [4,] TRUE FALSE FALSE FALSE FALSE
(m.1N <- as.matrix(L.1N))
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] FALSE FALSE FALSE FALSE TRUE
#> [2,] FALSE FALSE TRUE FALSE FALSE
#> [3,] FALSE FALSE FALSE FALSE FALSE
#> [4,] TRUE FALSE FALSE FALSE FALSE
stopifnot(identical(10L, which(is.na(m.0N))), !anyNA(m.1N))
symnum(m.0N)
#>
#> [1,] . . . . |
#> [2,] . . ? . .
#> [3,] . . . . .
#> [4,] | . . . .
symnum(m.1N)
#>
#> [1,] . . . . |
#> [2,] . . | . .
#> [3,] . . . . .
#> [4,] | . . . .