Rev 1866 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
R : Copyright 2005, The R Foundation for Statistical ComputingVersion 2.2.0 beta (2005-09-27 r35689)ISBN 3-900051-07-0R is free software and comes with ABSOLUTELY NO WARRANTY.You are welcome to redistribute it under certain conditions.Type 'license()' or 'licence()' for distribution details.R is a collaborative project with many contributors.Type 'contributors()' for more information and'citation()' on how to cite R or R packages in publications.Type 'demo()' for some demos, 'help()' for on-line help, or'help.start()' for a HTML browser interface to help.Type 'q()' to quit R.> #### For both 'Extract' ("[") and 'Replace' ("[<-") Method testing>> library(Matrix)>> source(system.file("test-tools.R", package = "Matrix"))# identical3() etc>> ### Dense Matrices>> m <- Matrix(1:28, nrow = 7)> validObject(m) ; m@x <- as.double(m@x) ; validObject(m)[1] TRUE[1] TRUE> stopifnot(identical(m, m[]),+ identical(m[2, 3], 16), # simple number+ identical(m[2, 3:4], c(16,23))) # simple numeric of length 2>> m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'1 x 2 Matrix of class "dgeMatrix"[,1] [,2][1,] 16 23> m[-(4:7), 3:4] # dito; the upper right corner of 'm'3 x 2 Matrix of class "dgeMatrix"[,1] [,2][1,] 15 22[2,] 16 23[3,] 17 24>> ## rows or columns only:> m[1,] # first row, as simple numeric vector[1] 1 8 15 22> m[,2] # 2nd column[1] 8 9 10 11 12 13 14> m[,1:2] # sub matrix of first two columns7 x 2 Matrix of class "dgeMatrix"[,1] [,2][1,] 1 8[2,] 2 9[3,] 3 10[4,] 4 11[5,] 5 12[6,] 6 13[7,] 7 14> m[-(1:6),, drop=FALSE] # not the first 6 rows, i.e. only the 7th1 x 4 Matrix of class "dgeMatrix"[,1] [,2] [,3] [,4][1,] 7 14 21 28>> ## logical indexing> stopifnot(identical(m[2,3], m[(1:nrow(m)) == 2, (1:ncol(m)) == 3]),+ identical(m[2,], m[(1:nrow(m)) == 2, ]),+ identical(m[,3:4], m[, (1:4) >= 3]))>> ## dimnames indexing:> mn <- m> dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),+ LETTERS[1:ncol(mn)])> mn["rd", "D"][1] 25> stopifnot(identical(mn["rc", "D"], mn[3,4]),+ identical(mn[, "A"], mn[,1]),+ identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])+ )>> mo <- m> m[2,3] <- 100> m[1:2, 4] <- 200> m[, 1] <- -1> m[1:3,]3 x 4 Matrix of class "dgeMatrix"[,1] [,2] [,3] [,4][1,] -1 8 15 200[2,] -1 9 100 200[3,] -1 10 17 24>> ## TODO: more --- particularly once we have "m > 10" working!>>> ### Sparse Matrices>> m <- 1:800> set.seed(101) ; m[sample(800, 600)] <- 0> m <- Matrix(m, nrow = 40)> mm <- as(m, "matrix")> dimnames(mm) <- NULL ## << workaround: as(<sparse>, "matrix") has NULL dimnames> str(mC <- as(m, "dgCMatrix"))Formal class 'dgCMatrix' [package "Matrix"] with 6 slots..@ i : int [1:200] 2 6 11 21 24 29 37 38 1 4 .....@ p : int [1:21] 0 8 22 28 37 41 50 63 71 81 .....@ Dim : int [1:2] 40 20..@ Dimnames:List of 2.. ..$ : NULL.. ..$ : NULL..@ factors : list()..@ x : num [1:200] 3 7 12 22 25 30 38 39 42 45 ...> str(mT <- as(m, "dgTMatrix"))Formal class 'dgTMatrix' [package "Matrix"] with 6 slots..@ i : int [1:200] 2 6 11 21 24 29 37 38 1 4 .....@ j : int [1:200] 0 0 0 0 0 0 0 0 1 1 .....@ Dim : int [1:2] 40 20..@ Dimnames:List of 2.. ..$ : NULL.. ..$ : NULL..@ factors : list()..@ x : num [1:200] 3 7 12 22 25 30 38 39 42 45 ...> stopifnot(identical(mT, as(mC, "dgTMatrix")),+ identical(mC, as(mT, "dgCMatrix")))>> mC[,1][1] 0 0 3 0 0 0 7 0 0 0 0 12 0 0 0 0 0 0 0 0 0 22 0 0 25[26] 0 0 0 0 30 0 0 0 0 0 0 0 38 39 0> mC[1:2,]2 x 20 sparse Matrix of class "dgCMatrix"[1,] . . . 121 . . 241 . . . . 441 . . 561 . 641 . . .[2,] . 42 . . . 202 . . . . . . 482 522 . . . . 722 .> mC[7, drop = FALSE]1 x 20 sparse Matrix of class "dgCMatrix"[1,] 7 . . . . . . 287 . . 407 . 487 527 . . . . 727 .> stopifnot(identical(mC[7, drop = FALSE],+ mC[7,, drop = FALSE]))> mT[,c(2,4)]40 x 2 sparse Matrix of class "dgTMatrix"[1,] . 121[2,] 42 .[3,] . .[4,] . .[5,] 45 .[6,] . .[7,] . .[8,] . 128[9,] . 129[10,] 50 .[11,] . .[12,] 52 132[13,] . 133[14,] . .[15,] 55 .[16,] . .[17,] . .[18,] . 138[19,] . .[20,] . .[21,] . 141[22,] . 142[23,] 63 .[24,] . .[25,] 65 .[26,] . .[27,] 67 .[28,] 68 .[29,] . .[30,] . .[31,] 71 .[32,] 72 .[33,] . .[34,] 74 .[35,] . .[36,] 76 .[37,] . .[38,] . .[39,] . 159[40,] 80 .> mT[1,][1] 0 0 0 121 0 0 241 0 0 0 0 441 0 0 561 0 641 0 0[20] 0> mT[4, drop = FALSE]1 x 20 sparse Matrix of class "dgTMatrix"[1,] . . . . . . . . 324 . . . 484 524 564 . . . . 764> stopifnot(identical3(mm[,1], mC[,1], mT[,1]),+ identical3(mm[3,], mC[3,], mT[3,]),+ identical3(mT[2,3], mC[2,3], 0),+ identical(mT[], mT),+ ## TODO: identical4() with m[c(3,7), 2:4] - fail because of 'dimnames'+ identical3(as(mC[c(3,7), 2:4],"matrix"), mm[c(3,7), 2:4],+ as(mT[c(3,7), 2:4],"matrix")))>> ## --- negative indices ----------> mc <- mC[1:5, 1:7]> mt <- mT[1:5, 1:7]> ## sub matrix> stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),+ identical(mt[-(3:5), 0:2], mT[1:2, 0:2]))> ## sub vector> stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],+ mt[-(1:4), ], mT[5, 1:7]))> stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)],+ mt[-(1:4), -(2:4)], mT[5, c(1,5:7)]))>> ## mixing of negative and positive must give error> assertError(mT[-1:1,])>> ## At least these now give a nicely understandable error:> try(mT[1, 4] <- -99)Error in "[<-"(`*tmp*`, 1, 4, value = -99) :not-yet-implemented 'Matrix[<-' method> try(mT[2:3, ] <- 0)Error in "[<-"(`*tmp*`, 2:3, , value = 0) :not-yet-implemented 'Matrix[<-' method>