4.3.2 Vectors
4.3.2.1 Creating Vectors
Simply put, vectors are lists of objects that are all of the same type. They can be the result of a calculation or be declared with the function c()
.
vector
x <- c(2, 2.5, 4, 6) y <- c("apple", "pear") class(x) ## [1] "numeric" class(y) ## [1] "character"
More about lists can be found in Section 4.3.6 " Lists " on page 53.
4.3.3 Accessing Data from a Vector
In many situations you will need to address a vector as a whole, but there will also be many occasions where it is necessary to do something with just one element. Since this is such a common situation, R provides more than one way to get this done.
# Create v as a vector of the numbers one to 5:v <- c(1 :5) # Access elements via indexing:v[2] ## [1] 2 v[ c(1,5)] ## [1] 1 5 # Access via TRUE/FALSE:v[ c(TRUE,TRUE,FALSE,FALSE,TRUE)] ## [1] 1 2 5 # Access elements via names:v <- c("pear" = "green", "banana" = "yellow", "coconut" = "brown") v ## pear banana coconut ## "green" "yellow" "brown" v["banana"] ## banana ## "yellow" # Leave out certain elements:v[ c( -2, -3)] ## pear ## "green"
4.3.3.1 Vector Arithmetic
The standard behaviour for vector arithmetic in R is element per element. With “standard” we mean operators that do not appear between percentage signs (as in %.%
for example).
v1 <- c(1,2,3) v2 <- c(4,5,6) # Standard arithmeticv1 +v2 ## [1] 5 7 9 v1 -v2 ## [1] -3 -3 -3 v1 *v2 ## [1] 4 10 18
Warning – Not all operations are element per element
The dot-product and other non-element-per-element-operators are available via specialized operators such as %.%
: see Section 4.4.1 “ Arithmetic Operators ” on page 75
Vector recycling refers to the fact that in case an operation is requested with one too short vector, that this vector will be concatenated with itself till it has the required length.
# Define a short and long vector:v1 <- c(1, 2, 3, 4, 5) v2 <- c(1, 2) # Note that R ‘recycles’ v2 to match the length of v1:v1 +v2 ## Warning in v1 + v2: longer object length is not a multiple of shorter object length ## [1] 2 4 4 6 6
Warning – Vector recycling
This behaviour is most probably different from what the experienced programmer will expect. Not only we can add or multiply vectors of different nature (e.g. long and real), but also we can do arithmetic on vectors of different size. This is usually not what you have in mind, and does lead to programming mistakes. Do take an effort to avoid vector recycling by explicitly building vectors of the right size.
4.3.3.3 Reordering and Sorting
To sort a vector, we can use the function sort()
.
sorting
sort()
# Example 1:v1 <- c(1, -4, 2, 0, pi) sort(v1) ## [1] -4.000000 0.000000 1.000000 2.000000 3.141593 # Example 2: To make sorting meaningful, all variables are coerced to # the most complex type:v1 <- c(1 :3, 2 +2i) sort(v1) ## [1] 1+0i 2+0i 2+2i 3+0i # Sorting is per increasing numerical or alphabetical order:v3 <- c("January", "February", "March", "April") sort(v3) ## [1] "April" "February" "January" "March" # Sort order can be reversed: sort(v3, decreasing = TRUE) ## [1] "March" "January" "February" "April"
Question #2 Temperature conversion
The time series nottem
(from the package “datasets” that is usually loadedwhen R starts) contains the temperatures in Notthingham from 1920 to 1939 in Fahrenheit. Create a new object that contains a list of all temperatures in Celsius.
Hint – Addressing the object nottem
Note that nottem
is a time series object (see Chapter 10“ Time Series Analysis ” on page 255) and not a matrix. Its elements are addressed with nottam[n]
where n is between 1 and length(nottam)
. However, when printed it will look like a matrix with months in the columns and years in the rows. This is because the print-function will use functionality specific to the time series object. a Remember that
).
temperature
length()
Matrices are a very important class of objects. They appear in all sorts of practical problems: investment portfolios, landscape rendering in games, image processing in the medical sector, fitting of neural networks, etc.
matrix
4.3.4.1 Creating Matrices
A matrix is in two-dimensional data set where all elements are of the same type. The matrix()
function offers a convenient way to define it:
matrix()
# Create a matrix.M = matrix( c(1 :6), nrow = 2, ncol = 3, byrow = TRUE) print(M) ## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6 M = matrix( c(1 :6), nrow = 2, ncol = 3, byrow = FALSE) print(M) ## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6
It is also possible to create a unit or zero vector with the same function. If we supply one scalar instead a vector to the first argument of the function matrix()
, it will be recycled as much as necessary.
matrix()
# Unit vector: matrix(1, 2, 1) ## [,1] ## [1,] 1 ## [2,] 1 # Zero matrix or vector: matrix(0, 2, 2) ## [,1] [,2] ## [1,] 0 0 ## [2,] 0 0 # Recycling also works for shorter vectors: matrix(1 :2, 4, 4) ## [,1] [,2] [,3] [,4] ## [1,] 1 1 1 1 ## [2,] 2 2 2 2 ## [3,] 1 1 1 1 ## [4,] 2 2 2 2 # Fortunately, R expects that the vector fits exactly n times in the matrix: matrix(1 :3, 4, 4) ## Warning in matrix(1:3, 4, 4): data length [3] is not a sub-multiple or multiple of the number of rows [4] ## [,1] [,2] [,3] [,4] ## [1,] 1 2 3 1 ## [2,] 2 3 1 2 ## [3,] 3 1 2 3 ## [4,] 1 2 3 1 # So, the previous was bound to fail.
4.3.4.2 Naming Rows and Columns
Читать дальше