https://github.com/cran/slam
Raw File
Tip revision: a2eb9cb8cac25421beb6ba64ef71b28d3f1e0335 authored by Kurt Hornik on 07 January 2014, 00:00:00 UTC
version 0.1-31
Tip revision: a2eb9cb
subassign.Rout.save

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: i686-pc-linux-gnu (32-bit)

R 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 an HTML browser interface to help.
Type 'q()' to quit R.

> ##
> library("slam")
> ## sparse
> x <- simple_sparse_zero_array(dim = c(3, 4, 2))
> 				    ## removal of subscripts
> k <- matrix(c(2, 1, 1, 0, 1, 1), c(2, 3), byrow = TRUE)
> k
     [,1] [,2] [,3]
[1,]    2    1    1
[2,]    0    1    1
> x[k] <- 1
> x[3, 1, 1] <- 2 
> x[c(17, 17)] <- c(2, 3)		    ## duplicate subscripts
> x[c(5, NA, 6)] <- 4		    ## recycling
> data.frame(v = x$v, i = x$i,
+     k = .Call("_vector_index", x$dim, x$i))
  v i.1 i.2 i.3  k
1 1   2   1   1  2
2 2   3   1   1  3
3 3   2   2   2 17
4 4   2   2   1  5
5 4   3   2   1  6
> 
> ##
> x[, -1, 1] <- 0			    ## zero elements
> data.frame(v = x$v, i = x$i,
+     k = .Call("_vector_index", x$dim, x$i))
  v i.1 i.2 i.3  k
1 1   2   1   1  2
2 2   3   1   1  3
3 3   2   2   2 17
> x[-c(2, 3)] <- 0
> data.frame(v = x$v, i = x$i,
+     k = .Call("_vector_index", x$dim, x$i))
  v i.1 i.2 i.3 k
1 1   2   1   1 2
2 2   3   1   1 3
> 
> x[] <- 0
> str(x)
List of 4
 $ i       : int[0 , 1:3] 
 $ v       : num(0) 
 $ dim     : int [1:3] 3 4 2
 $ dimnames: NULL
 - attr(*, "class")= chr "simple_sparse_array"
> 
> ## misc
> x[integer()] <- 1
> x[matrix(integer(), nrow = 0, ncol = 3)] <- 1
> str(x)
List of 4
 $ i       : int[0 , 1:3] 
 $ v       : num(0) 
 $ dim     : int [1:3] 3 4 2
 $ dimnames: NULL
 - attr(*, "class")= chr "simple_sparse_array"
> 
> try(x[c(NA, 2, 3)] <- 1:2)	    ## not allowed
Error in `[<-.simple_sparse_array`(`*tmp*`, c(NA, 2, 3), value = 1:2) : 
  NAs are not allowed in subscripted assignments
> ## works with R >= 3.x
> try(x[-c(.Machine$integer.max + 1, 1)] <- c(1, 2))
Warning message:
In `[<-.simple_sparse_array`(`*tmp*`, -c(.Machine$integer.max +  :
  number of items to replace is not a multiple of replacement length
> 
> 
> ## reference
> x <- matrix(1:6, nrow = 3)
> 
> ## matrix indexing
> k <- matrix(c(1, 1, 2, 2, 1, 1), ncol = 2, byrow = TRUE)
> k
     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]    1    1
> 
> z <- x
> z[k] <- -1
> z
     [,1] [,2]
[1,]   -1    4
[2,]    2   -1
[3,]    3    6
> 
> z <- x
> z[k] <- -(1:3)			    ## last in sequence 
> z
     [,1] [,2]
[1,]   -3    4
[2,]    2   -2
[3,]    3    6
> 
> ## implicit vector indexing
> k <- matrix(k, nrow = 2)
> as.vector(k)
[1] 1 2 1 1 2 1
> 
> z <- x
> z[k] <- -1
> z
     [,1] [,2]
[1,]   -1    4
[2,]   -1    5
[3,]    3    6
> 
> z <- x
> z[k] <- -(1:6)			    ## last in sequence
> z
     [,1] [,2]
[1,]   -6    4
[2,]   -5    5
[3,]    3    6
> 
> ## missing values
> z <- x
> z[c(NA, 1, 2)] <- -1
> z
     [,1] [,2]
[1,]   -1    4
[2,]   -1    5
[3,]    3    6
> 
> z <- x
> try(z[c(NA, 1, 2)] <- -(1:2))	    ## not allowed
Error in z[c(NA, 1, 2)] <- -(1:2) : 
  NAs are not allowed in subscripted assignments
> 
> k[1L] <- NA			    ## implicit vector indexing
> as.vector(k)
[1] NA  2  1  1  2  1
> z <- x
> z[k] <- -1
> z
     [,1] [,2]
[1,]   -1    4
[2,]   -1    5
[3,]    3    6
> 
> k <- matrix(c(NA, 1, 1, 1, 2, 2), ncol = 2, byrow = TRUE)
> k
     [,1] [,2]
[1,]   NA    1
[2,]    1    1
[3,]    2    2
> 
> z <- x
> z[k] <- -1
> z
     [,1] [,2]
[1,]   -1    4
[2,]    2   -1
[3,]    3    6
> 
> z <- x
> try(z[k] <- -(1:2))		    ## not allowed
Error in z[k] <- -(1:2) : NAs are not allowed in subscripted assignments
> 
> ## zeros
> z <- x
> z[c(0, 1)] <- -1
> z
     [,1] [,2]
[1,]   -1    4
[2,]    2    5
[3,]    3    6
> 
> z <- x
> z[c(0, 1)] <- -(1:2)
Warning message:
In z[c(0, 1)] <- -(1:2) :
  number of items to replace is not a multiple of replacement length
> z
     [,1] [,2]
[1,]   -1    4
[2,]    2    5
[3,]    3    6
> 
> k <- matrix(c(1, 1, 0, 2), ncol = 2, byrow = TRUE)
> k
     [,1] [,2]
[1,]    1    1
[2,]    0    2
> 
> z <- x
> z[k] <- -1
> z
     [,1] [,2]
[1,]   -1    4
[2,]    2    5
[3,]    3    6
> 
> z <- x
> z[k] <- -(1:2)
Warning message:
In z[k] <- -(1:2) :
  number of items to replace is not a multiple of replacement length
> z
     [,1] [,2]
[1,]   -1    4
[2,]    2    5
[3,]    3    6
> 
> ## extending
> k <- matrix(c(1, 4), ncol = 2)
> 
> z <- x
> try(z[k] <- 1)			    ## not allowed
Error in z[k] <- 1 : subscript out of bounds
> 
> z[c(1, 8)] <- 1			    ## not implemented
> z
[1]  1  2  3  4  5  6 NA  1
> 
> ## misc
> z <- x
> try(z[-c(.Machine$integer.max + 1, 1)] <- c(1, 2))
Warning message:
In z[-c(.Machine$integer.max + 1, 1)] <- c(1, 2) :
  number of items to replace is not a multiple of replacement length
> 
> ###
> 
> proc.time()
   user  system elapsed 
  0.356   0.036   0.385 
back to top