4.1 Numeric vectors
A vetcor containing decimal numbers (e.g. 3.1415926) is of type numeric
.
Note that R derives the data type of the variable implicitly from the object assigned to the variable. In the case of x <- 3.46
, for instance, we don’t need R to tell that 3.46
is numeric - it will automatically infer this itself.
Just as integer vectors, numeric vectors are also created using the c()
function, or by using the function numeric()
that creates a numeric vector of specified length with all elements set to zero.
Note that since vectors can only hold elements of a single type, the c()
function coerces all elements to a common type.
> x <- c(1,5)
> class(x)
[1] "numeric"
> x
[1] 1 5
> x <- c(x, 2.5)
> class(x)
[1] "numeric"
> x
[1] 1.0 5.0 2.5
You can coerce vectors to a specific type using the as.integer()
or as.numeric()
function.
As you can see, an integer vector can only hold integer numbers and a coercing of decimal numbers results in a loss of the information past the decimal point (no, the numbers are not rounded).
4.1.1 Arithmetic Operations with Vectors
Numeric vectors can be used in arithmetic expressions, in which case the operations are performed element by element:
> x <- 1:4
> y <- c(1.1, 1.2, 1.3, 1.4)
> x+y
[1] 2.1 3.2 4.3 5.4
> x^y
[1] 1.000000 2.297397 4.171168 6.964405
If two vectors are of different length, the shorter of the two is recycled. This means that the values from the shorter vector are simply re-used until the length of the longer one is reached. If R can not fully recycle the shorter vector, it will display a warning message but still do the operation.
> x <- 1:4
> y <- 0:1
> x*y
[1] 0 2 0 4
> y <- 0:2
> x*y
Warning in x * y: longer object length is not a multiple of shorter object
length
[1] 0 2 6 0
Recycling appears in many contexts, and it’s good to know about it. For example, we can make use of recycling to set all even numbers to 0:
… and much more!
Apart from elementary arithmetic operators (+
, -
, *
, /
, %%
, ^
) discussed above, all of the common arithmetic functions are available, for example log()
, exp()
, sin()
, cos()
, tan()
, sqrt()
etc. These functions operate element-by-element and return a vector of same length as the input vector:
> x <- 1:10
> log(x)
[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379 1.7917595 1.9459101
[8] 2.0794415 2.1972246 2.3025851
> sqrt(x)
[1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
[9] 3.000000 3.162278
Other functions take the whole vector as an argument, rather than applying the function to each element. These functions then return a single value. Examples are mean()
, sd()
, var()
, median()
, min()
, max()
, …
> x <- 1:10
> mean(x)
[1] 5.5
> sd(x)
[1] 3.02765
> var(x)
[1] 9.166667
> median(x)
[1] 5.5
> min(x)
[1] 1
> max(x)
[1] 10
Note that R automatically coerces types where necessary. This is illustrated in the following example: when adding two integer vectors, the result is also an integer vector. But when adding an integer and a numeric vector, the result is a numeric vector. Similarly, when dividing two integer vectors, the result is of type numeric.
4.1.2 Exercises: Numeric vectors
See Section 18.0.6 for solutions.
Calculate the differences, sums and product between the elements of vectors
a
, containing the values from -5 to 5, andb
, containing values from 10 down to 0, respectively.Calculate the sum of all elements in
a
andb
.Identify the largest and smallest values of
a
and compute the overall mean.Standardize
a
such that it has mean 0 and sd 1. Test your googling-skills if you’re not sure how to do this.Use recycling to multiply every 3rd element of
a
with 5. Why do you get a warning message?