Last data update: 2014.03.03

R: Sparse Vector Construction from Nonzero Entries
sparseVectorR Documentation

Sparse Vector Construction from Nonzero Entries

Description

User friendly construction sparse vectors, i.e., objects inheriting from class sparseVector, from indices and values of its non-zero entries.

Usage

sparseVector(x, i, length)

Arguments

x

vector of the non zero entries.

i

integer vector (of the same length as x) specifying the indices of the non-zero (or non-TRUE) entries of the sparse vector.

length

length of the sparse vector.

Details

zero entries in x are dropped automatically, analogously as drop0() acts on sparse matrices.

Value

a sparse vector, i.e., inheriting from class sparseVector.

Author(s)

Martin Maechler

See Also

sparseMatrix() constructor for sparse matrices; the class sparseVector.

Examples

str(sv <- sparseVector(x = 1:10, i = sample(999, 10), length=1000))

sx <- c(0,0,3, 3.2, 0,0,0,-3:1,0,0,2,0,0,5,0,0)
ss <- as(sx, "sparseVector")
stopifnot(identical(ss,
   sparseVector(x = c(2, -1, -2, 3, 1, -3, 5, 3.2),
                i = c(15L, 10:9, 3L,12L,8L,18L, 4L), length = 20L)))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-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(Matrix)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Matrix/sparseVector.Rd_%03d_medium.png", width=480, height=480)
> ### Name: sparseVector
> ### Title: Sparse Vector Construction from Nonzero Entries
> ### Aliases: sparseVector
> ### Keywords: array
> 
> ### ** Examples
> 
> str(sv <- sparseVector(x = 1:10, i = sample(999, 10), length=1000))
Formal class 'isparseVector' [package "Matrix"] with 3 slots
  ..@ x     : int [1:10] 7 5 4 3 1 2 9 8 6 10
  ..@ length: num 1000
  ..@ i     : int [1:10] 12 75 201 202 330 359 366 482 664 957
> 
> sx <- c(0,0,3, 3.2, 0,0,0,-3:1,0,0,2,0,0,5,0,0)
> ss <- as(sx, "sparseVector")
> stopifnot(identical(ss,
+    sparseVector(x = c(2, -1, -2, 3, 1, -3, 5, 3.2),
+                 i = c(15L, 10:9, 3L,12L,8L,18L, 4L), length = 20L)))
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>