Vectors
Vectors of numerical values can be created with assignment statements of the form
variable <- type(length);
type can be double, complex or integer. If the length parameter is specified,
then a vector of that size is created and the values are initialized to zero. If length is omitted, then
the vector is sized appropriately when an element is accessed.
Indexing starts at 1.
Elements of the vector are accessed using []'s. A range of indices can be accessed using the : notation, e.g. z[1:3]
refers the first three elements of z collectively.
Vectors are "resized on demand"; if an index is requested that is larger than the current size of the
vector, the vector will automatically be resized to allow for an element with that index.
length(z)
returns the size of the vector.
remove(z)
removes the vector from the workspace.
Alternate methods of vector construction :
x <- seq(1,20,by = 3.0);
# To load a sequence.
x <- c(0.0,1.0,2.0);
# Using the c command (concatenate) to create a vector.
Binary operations work on vectors, however, be aware that operations on vectors of differing lengths will not give
an error; as the values of the shorter vectors are re-cycled.
See Also : Operators
Original Documentation : vector, remove,
c
, seq
Samples
v <-double(3); v [1] 0 0 0 z <- complex(4); z [1] 0+0i 0+0i 0+0i 0+0i k <- integer(2); k [1] 0 0
# implicit construction using the automatic sizing feature
w <- double(); w[5] <- 3.0; w [1] NA NA NA NA 3
# range access
z[1:3] <- 5.0; z [1] 5 5 5 0 0
# create a vector using concatenate
z <- c(0.0,1.4,4.4); z [1] 0.0 1.4 4.4
# create a vector using the sequence command
z <- seq(0.0,1.0,.1); z [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
# adding two vectors together
x <- c(0.0,1.0); y <- c(1.0,1.0); x [1] 0 1 y [1] 1 1 x+y [1] 1 2
UCLA Mathematics Department | ©2000 |