Vectors
Vectors are an ordered container data type.
Contents
Instantiation
Vectors are commonly instantiated using the 'concatenation' function.
x <- c(1,2,3)
To instantiate a null numeric vector of some size, try:
y <- double(length=100)
Note that numeric is an alias for the same double function.
To instantiate a null character vector of some size, try:
z <- character(length=100)
Often there is a more appropriate default value than "" however, so consider instead using:
z <- rep("foo", times = 100)
Accessing elements
Elements of a vector are accessed by indexing; note that vectors index from 1.
first <- x[1] last <- x[length(x)] second_and_third <- x[c(2,3)] middle <- x[2:length(x)-1]
Assignment to elements works as expected.
x[2] <- 1 x[1] <- x[2] x[c(2,3)] <- c(1,2)
Assigning to a non-existent element causes the vector to be extended as necessary, with null values at all indices between the 'old' last index and the 'new' last index.
Negative indexing causes the selected elements to be censored.
all_but_first <- x[-1] all_but_second_and_third <- x[-c(2,3)]
Indexing by zero evaluates as an empty vector (i.e., length of 0) that is of the corresponding type.
