The Big R-Book. Philippe J. S. De Brouwer

Чтение книги онлайн.

Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 27

The Big R-Book - Philippe J. S. De Brouwer

Скачать книгу

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 arithmetic v1 + v2 ## [1] 5 7 9 v1 - v2 ## [1] -3 -3 -3 v1 * v2 ## [1] 4 10 18

      image 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

      4.3.3.2 Vector Recycling

      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

      image Warning – Vector recycling

      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"

      image 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.

      image Hint – Addressing the object nottem

       temperature

       length()

      4.3.4 Matrices

       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

      row_names = c("row1", "row2", "row3", "row4") col_names = c("col1", "col2", "col3") M <- matrix(c(10:21), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names)) print(M) ## col1 col2 col3 ## row1 10 11 12 ## row2 13 14 15 ## row3 16 17 18 ## row4 19 20 21

       dimnames

      Once thematrix exists, the

Скачать книгу