• No results found

Subsets, Extractions, and Replacements

In document The Book of R (Page 89-94)

3.4 Multidimensional Arrays

3.4.2 Subsets, Extractions, and Replacements

Even though high-dimensional objects can be difficult to conceptualize, R indexes them consistently. This makes extracting elements from these structures straightforward now that you know how to subset matrices—you just have to keep using commas in the square brackets as separators of the dimensions being accessed. This is highlighted in the examples that follow.

Suppose you want the second row of the second layer of the previously created arrayAR. You just enter these exact dimensional locations ofARin square brackets.

R> AR[2,,2] [1] 14 17 20 23

The desired elements have been extracted as a vector of length 4. If you want specific elements from this vector, say the third and first, in that order, you can call the following:

R> AR[2,c(3,1),2] [1] 20 14

Again, this literal method of subsetting makes dealing with even high- dimensional objects in R manageable.

An extraction that results in multiple vectors will be presented as columns in the returned matrix. For example, to extract the first rows of both layers ofAR, you enter this:

R> AR[1,,] [,1] [,2] [1,] 1 13

[2,] 4 16 [3,] 7 19 [4,] 10 22

The returned object has the first rows of each of the two matrix layers. However, it has returned each of these vectors as a column of the single returned matrix. As this example shows, when multiple vectors are extracted from an array, they will be returned as columns by default. This means extracted rows will not necessarily be returned as rows.

Turning to the objectBR, the following gives you the single element of the second row and first column of the matrix in the first layer of the three- dimensional array located in the third block.

R> BR[2,1,1,3] [1] 2

Again, you just need to look at the position of the index in the square brackets to know which values you are asking R to return from the array. The following examples highlight this:

R> BR[1,,,1] [,1] [,2] [1,] 1 13 [2,] 4 16 [3,] 7 19 [4,] 10 22

This returns all the values in the first row of the first block. Since I left the column and layer indexes blank in this subset[1,,,1], the command has returned values for all four columns and both layers in that block ofBR.

Next, the following line returns all the values in the second layer of the arrayBR, composed of three matrices:

R> BR[,,2,] , , 1 [,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24 , , 2 [,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24 56 Chapter 3

, , 3

[,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24

This last example highlights a feature noted earlier, where multiple vectors fromARwere returned as a matrix. Broadly speaking, if you have an extraction that results in multiple d-dimensional arrays, the result will be an array of the next-highest dimension, d + 1. In the last example, you extracted multiple (two-dimensional) matrices, and they were returned as a three-dimensional array. This is demonstrated again in the next example:

R> BR[3:2,4,,] , , 1 [,1] [,2] [1,] 12 24 [2,] 11 23 , , 2 [,1] [,2] [1,] 12 24 [2,] 11 23 , , 3 [,1] [,2] [1,] 12 24 [2,] 11 23

This extracts the elements at rows 3 and 2 (in that order), column 4, for all layers and for all array blocks. Consider the following final example:

R> BR[2,,1,] [,1] [,2] [,3] [1,] 2 2 2 [2,] 5 5 5 [3,] 8 8 8 [4,] 11 11 11

Here you’ve asked R to return the entire second rows of the first layers of all the arrays stored inBR.

Deleting and overwriting elements in high-dimensional arrays follows the same rules as for stand-alone vectors and matrices. You specify the

dimension positions the same way, using negative indexes (for deletion) or using the assignment operator for overwriting.

You can use thearrayfunction to create one-dimensional arrays (vec- tors) and two-dimensional arrays (matrices) should you want to (by setting thedimargument to be of length 1 or 2, respectively). Note, though, that vectors in particular may be treated differently by some functions if created witharrayinstead ofc(see the help file?arrayfor technical details). For this reason, and to make large sections of code more readable, it’s more con- ventional in R programming to use the specific vector- and matrix-creation functionscandmatrix.

Exercise 3.3

a. Create and store a three-dimensional array with six layers of a 4 × 2 matrix, filled with a decreasing sequence of values between 4.8 and 0.1 of the appropriate length.

b. Extract and store as a new object the fourth- and first-row ele- ments, in that order, of the second column only of all layers of (a).

c. Use a fourfold repetition of the second row of the matrix formed in (b) to fill a new array of dimensions 2 × 2 × 2 × 3.

d. Create a new array comprised of the results of deleting the sixth layer of (a).

e. Overwrite the second and fourth row elements of the second column of layers 1, 3, and 5 of (d) with −99.

Important Code in This Chapter

Function/operator Brief description First occurrence matrix Create a matrix Section 3.1, p. 40 rbind Create a matrix (bind rows) Section 3.1.2, p. 41 cbind Create a matrix (bind columns) Section 3.1.2, p. 42 dim Get matrix dimensions Section 3.1.3, p. 42 nrow Get number of rows Section 3.1.3, p. 42 ncol Get number of columns Section 3.1.3, p. 42 [ , ] Matrix/array subsetting Section 3.2, p. 43 diag Diagonal elements/identity matrix Section 3.2.1, p. 44 t Matrix transpose Section 3.3.1, p. 47 * Scalar matrix multiple Section 3.3.3, p. 49 +,- Matrix addition/subtraction Section 3.3.4, p. 49 %*% Matrix multiplication Section 3.3.5, p. 50 solve Matrix inversion Section 3.3.6, p. 51 array Create an array Section 3.4.1, p. 53

4

N O N - N U M E R I C V A L U E S

So far, you’ve been working almost exclu-

sively with numeric values. But statistical

programming also requires non-numeric

values. In this chapter, we’ll consider three

important non-numeric data types: logicals, char-

acters, and factors. These data types play an impor-

tant role in effective use of R, especially as we get into

more complex R programming in Part II.

4.1 Logical Values

Logical values (also simply called logicals) are based on a simple premise: a logical-valued object can only be eitherTRUEorFALSE. These can be inter- preted as yes/no, one/zero, satisfied/not satisfied, and so on. This is a con- cept that appears across all programming languages, and logical values have many important uses. Often, they signal whether a condition has been satis- fied or whether a parameter should be switched on or off.

You encountered logical values briefly when you used thesortfunction in Section 2.3.2 and thematrixfunction in Section 3.1. When usingsort, set- tingdecreasing=TRUEreturns a vector ordered from largest to smallest, and

decreasing=FALSEsorts the vector the other way around. Similarly, when con- structing a matrix,byrow=TRUEfills the matrix entries row-wise; otherwise, the matrix is filled column-wise. Now, you’ll take a more detailed look at ways to use logicals.

In document The Book of R (Page 89-94)