How to Add a Second Dimension in R
In addition to vectors, R can represent matrices as an object you work and calculate with. In fact, R really shines when it comes to matrix calculations and operations.Vectors are closely related to a bigger class of objects, arrays. Arrays have two very important features:
They contain only a single type of value.
They have dimensions.
The dimensions of an array determine the type of the array. You know already that a vector has only one dimension. An array with two dimensions is a matrix. Anything with more than two dimensions is simply called an array.
Technically, a vector has no dimensions at all in R. If you use the functions dim(), nrow(), or ncol() with a vector as argument, R returns NULL as a result.
How to create your first matrix in R
Creating a matrix is almost as easy as writing the word: You simply use the matrix() function. You do have to give R a little bit more information, though. R needs to know which values you want to put in the matrix and how you want to put them in. The matrix() function has a couple arguments to control this:
data is a vector of values you want in the matrix.
ncol takes a single number that tells R how many columns you want.
nrow takes a single number that tells R how many rows you want.
byrow takes a logical value that tells R whether you want to fill the matrix row-wise (TRUE) or column-wise (FALSE). Column-wise is the default.
So, the following code results in a matrix with the numbers 1 through 12, in four columns and three rows.
> first.matrix <- matrix(1:12, ncol=4) > first.matrix [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12
You don’t have to specify both ncol and nrow. If you specify one, R will know automatically what the other needs to be.
Alternatively, if you want to fill the matrix row by row, you can do so:
> matrix(1:12, ncol=4, byrow=TRUE) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12
Matrix properties in R
You can look at the structure of an object using the str() function. If you do that for your first matrix, you get the following result:
> str(first.matrix) int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
This looks remarkably similar to the output for a vector, with the difference that R gives you both the indices for the rows and for the columns. If you want the number of rows and columns without looking at the structure, you can use the dim() function.
> dim(first.matrix)  3 4
To get only the number of rows, you use the nrow() function. The ncol() function gives you the number of columns of a matrix.
You can find the total number of values in a matrix exactly the same way as you do with a vector, using the length() function:
> length(first.matrix)  12
Actually, if you look at the output of the str() function, that matrix looks very much like a vector. That’s because, internally, it’s a vector with a small extra piece of information that tells R the dimensions (see the nearby sidebar, “Playing with attributes”). You can use this property of matrices in calculations.