March 2018
Intermediate to advanced
816 pages
19h 35m
English
A matrix is a two-dimensional array of values of the same type, or mode. You generate matrices from vectors with the matrix() function. Columns and rows can have labels. You can generate a matrix from a vector by rows or by columns (default). The following code shows some matrices and the difference of the generation, by rows or by columns:
x = c(1,2,3,4,5,6); x;
Y = array(x, dim=c(2,3)); Y;
Z = matrix(x,2,3,byrow=F); Z
U = matrix(x,2,3,byrow=T); U; # A matrix - fill by rows
rnames = c("Row1", "Row2");
cnames = c("Col1", "Col2", "Col3");
V = matrix(x,2,3,byrow=T, dimnames = list(rnames, cnames)); V;
The first line generates and shows a one-dimensional vector. The second line creates a two-dimensional array, ...