Chapter 4. Vectors, Matrices, and Arrays
In Chapters 1 and 2, we saw several types of vectors for logical values, character strings, and of course numbers. This chapter shows you more manipulation techniques for vectors and introduces their multidimensional brethren, matrices and arrays.
Chapter Goals
After reading this chapter, you should:
- Be able to create new vectors from existing vectors
- Understand lengths, dimensions, and names
- Be able to create and manipulate matrices and arrays
Vectors
So far, you have used the colon operator, :, for creating sequences from one number to another, and the c function for concatenating values and vectors to create longer vectors. To recap:
8.5:4.5#sequence of numbers from 8.5 down to 4.5
## [1] 8.5 7.5 6.5 5.5 4.5
c(1,1:3,c(5,8),13)#values concatenated into single vector
## [1] 1 1 2 3 5 8 13
The vector function creates a vector of a specified type and length. Each of the values in the result is zero, FALSE, or an empty string, or whatever the equivalent of “nothing” is:
vector("numeric",5)
## [1] 0 0 0 0 0
vector("complex",5)
## [1] 0+0i 0+0i 0+0i 0+0i 0+0i
vector("logical",5)
## [1] FALSE FALSE FALSE FALSE FALSE
vector("character",5)
## [1] "" "" "" "" ""
vector("list",5)
## [[1]] ## NULL ## ## [[2]] ## NULL ## ## [[3]] ## NULL ## ## [[4]] ## NULL ## ## [[5]] ## NULL
In that last example, NULL is a special “empty” value (not to be confused with NA, which indicates a missing data point). We’ll look at NULL in detail in Chapter 5. For convenience, ...