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

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

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

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

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

## [1] 3 # Sums of rows and columns: colSums(M) ## [1] 6 6 6 srowSums(M) ## [1] 5 5 8 # Means of rows, columns, and matrix: colMeans(M) ## [1] 2 2 2 rowMeans(M) ## [1] 1.666667 1.666667 2.666667 mean(M) ## [1] 2 # Horizontal and vertical concatenation: rbind(M, M) ## [,1] [,2] [,3] ## [1,] 1 1 3 ## [2,] 1 2 2 ## [3,] 4 3 1 ## [4,] 1 1 3 ## [5,] 1 2 2 ## [6,] 4 3 1 cbind(M, M) ## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 1 1 3 1 1 3 ## [2,] 1 2 2 1 2 2 ## [3,] 4 3 1 4 3 1

      4.3.5 Arrays

      Matrices are very useful, however there will be times that data has more dimensions than just two. R has a solutions with the base-type “array.” Unlike matrices that have always two dimensions, arrays can be of any number of dimensions. However, the requirement that all elements are of the same data-type is also valid for arrays.

       array

      Note that words like “array” are used as keywords in many computer languages, and that it is important to understand exactly how it is implemented in the language that you want to use. In this section we will introduce you to the practical aspects of working with arrays.

      4.3.5.1 Creating and Accessing Arrays

      Arrays can be created with the array() function; this function takes a “dim” attribute which defines the number of dimension. While arrays are similar to lists, they have to be of one class type (lists can consist of different class types).

       array()

      In the example we create an array with two elements, which are both three by three matrices.

      # Create an array: a <- array(c(‘A’,‘B’),dim = c(3,3,2)) print(a) ## , , 1 ## ## [,1] [,2] [,3] ## [1,] "A" "B" "A" ## [2,] "B" "A" "B" ## [3,] "A" "B" "A" ## ## , , 2 ## ## [,1] [,2] [,3] ## [1,] "B" "A" "B" ## [2,] "A" "B" "A" ## [3,] "B" "A" "B" # Access one element: a[2,2,2] ## [1] "B" # Access one layer: a[,,2] ## [,1] [,2] [,3] ## [1,] "B" "A" "B" ## [2,] "A" "B" "A" ## [3,] "B" "A" "B"

      4.3.5.2 Naming Elements of Arrays

      # Create two vectors: v1 <- c(1,1) v2 <- c(10:13) col.names <- c("col1","col2", "col3") row.names <- c("R1","R2") matrix.names <- c("Matrix1","Matrix2") # Take these vectors as input to the array. a <- array(c(v1,v2),dim = c(2,3,2), dimnames = list(row.names,col.names, matrix.names)) print(a) # This allows to address the first row in Matrix 1 as follows: a[‘R1’,,‘Matrix1’]

      4.3.5.3 Manipulating Arrays

      Using arrays and accessing its elements is in many aspects similar to working with matrices.

      M1 <- a[,,1] M2 <- a[,,2] M2 ## col1 col2 col3 ## R1 1 10 12 ## R2 1 11 13

      4.3.5.4 Applying Functions over Arrays

      While it is possible to use a for-loop and cycle through the elements of an array, it is usually faster to use the built-in functions that R provides.

      An efficient way to apply the same function over each element of an array is via the function apply(): that functions is designed to do exactly that.

       apply()

      Function use for apply()

      apply(X, MARGIN, FUN, …) with:

      1 X: an array, including a matrix.

      2 MARGIN: a vector giving the subscripts which the function will be applied over. E.g., for a matrix ‘1’ indicates rows, ‘2’ indicates columns, ‘c(1, 2)’ indicates rows and columns. Where ‘X’ has named dimnames, it can be a character vector selecting dimension names.

      3 FUN: the function to be applied: see ‘Details’. In the case of functions like ‘+’, ‘backquoted or quoted

      It is sufficient to provide the data, the dimension of application and the function that has to be applied. To show how this works, we construct a simple example to calculate sums of rows and sums of columns.

       cbind()

      x <- cbind(x1 = 3, x2 = c(4:1, 2:5)) dimnames(x)[[1]] <- letters[1:8] apply(x, 2, mean, trim = .2) ## x1 x2 ## 3 3 col.sums <- apply(x, 2, sum) row.sums <- apply(x, 1, sum) rbind(cbind(x, Rtot = row.sums), Ctot = c(col.sums, sum(col.sums))) ## x1 x2 Rtot ## a 3 4 7 ## b 3 3 6 ## c 3 2 5 ## d 3 1 4 ## e 3 2 5 ## f 3 3 6 ## g 3 4 7 ## h 3 5 8 ## Ctot 24 24 48

      The reader will notice that in the example above the variable x is actually not an array but rather a data frame. The function apply() works however the same: instead of 2 dimensions, there can be more.

       apply()

      Consider the previous example with the array a, and remember that a has three dimensions: 2 rows, 3 columns, and 2 matrices, then the following should be clear.

      # Re-create the array a (shorter code): col.names <- c("col1","col2", "col3") row.names <- c(“R1”,“R2”) matrix.names <- c(“Matrix1”,“Matrix2”) a <- array(c(1,1,10:13),dim = c(2,3,2), dimnames = list(row.names,col.names, matrix.names)) # Demonstrate apply: apply(a, 1, sum) ## R1 R2 ## 46 50 apply(a, 2, sum) ## col1 col2 col3 ## 4 42 50 apply(a, 3, sum) ## Matrix1 Matrix2 ## 48 48

      4.3.6 Lists

       list

      4.3.6.1 Creating Lists

       list()

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