Array has dimensions. A vector is an array with 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.

Creating matrix

use the matrix() function.

The matrix() function has arguments to specify the matrix.

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 rowwise

(TRUE) or column-wise (FALSE). Column-wise is the default.

You don’t have to specify both ncol and nrow. If you specify one, R will know automatically what the other needs to be.

You can look at the structure of an object using the str() function.

If you want the number of rows and columns without looking at the structure, you can use the

dim() function.

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:

To see all the attributes of an object, you can use the attributes() function

You can combine both vectors as two rows of a matrix with the rbind() function,

The cbind() function does something similar. It binds the vectors as columns of a matrix,

you have the functions rownames() and colnames(). Both functions work much like the names() function you use when naming vector values.

## Calculating with Matrices

You add a scalar to a matrix simply by using the addition operator, +,

With the addition operator, you can add both matrices together, if the

dimensions of both matrices are not the same, R will complain and refuse to carry

out the operation

By default, R fills matrices column-wise. Whenever R reads a matrix, it also

reads it column-wise.

Transposing a matrix

The t() function (which stands for transpose) will do the work

To

invert a matrix, you use the solve() function

The multiplication operator (*) works element-wise on matrices. To calculate

the inner product of two matrices, use the special operator %*%,

Reference

R for Dummies by Vries and Meys

chap 7

## No comments:

## Post a Comment