Vectors can be used in arithmetic expressions, in which case the operations are performed element by element. Vectors occurring in the same expression need not all be of the same length. If they are not, the value of the expression is a vector with the same length as the longest vector which occurs in the expression. Shorter vectors in the expression are recycled as often as need be (perhaps fractionally) until they match the length of the longest vector. In particular a constant is simply repeated. So with the above assignments the command
v <- 2*x + y + 1
generates a new vector v of length
constructed by
adding together, element by element, 2*x repeated
times, y repeated just once, and 1 repeated
times.
The elementary arithmetic operators are the usual , -, , / and ^ for raising to a power. In addition all of the common arithmetic functions are available. log, exp, sin, cos, tan, sqrt, and so on, all have their usual meaning. max and min select the largest and smallest elements of an vector respectively. range is a function whose value is a vector of length two, namely c(min(x), max(x)). length(x) is the number of elements in x, sum(x) gives the total of the elements in x and prod(x) their product.
Two statistical functions are mean(x) which calculates the sample mean, which is the same as sum(x)/length(x), and var(x) which gives sum((x-mean(x))2)/(length(x)-1)
or sample variance. If the argument to var() is an
matrix the value is a
sample covariance matrix got by regarding
the rows as independent
variate sample vectors.
sort(x) returns a vector of the same size as x with the elements arranged in increasing order; however there are other more flexible sorting facilities available (see order() or sort.list() which produce a permutation to do the sorting).
rnorm(x) is a function which generates a vector (or more generally an array) of pseudo-random standard normal deviates, of the same size as x.