Vectors and Simple Manipulations
This section introduces the basic operations on vectors, most of which are done element-wise. Please pay attention to the recycling of vectors (usually, recycling doesn't generate an error or a warning, so it is easy to miss if it was unintended), missing values (NA), and logical vectors often used for data subsetting.
Vector arithmetic
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 11 constructed by adding
together, element by element, 2*x
repeated 2.2 times, y
repeated just once, and 1
repeated 11 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 a
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
n-by-p matrix the value is a p-by-p sample
covariance matrix got by regarding the rows as independent
p-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).
Note that max
and min
select the largest and smallest
values in their arguments, even if they are given several vectors. The
parallel maximum and minimum functions pmax
and
pmin
return a vector (of length equal to their longest argument)
that contains in each element the largest (smallest) element in that
position in any of the input vectors.
For most purposes the user will not be concerned if the "numbers" in a numeric vector are integers, reals or even complex. Internally calculations are done as double precision real numbers, or double precision complex numbers if the input data are complex.
To work with complex numbers, supply an explicit complex part. Thus
sqrt(-17)
will give NaN
and a warning, but
sqrt(-17+0i)
will do the computations as complex numbers.