concept matrix in category R

This is an excerpt from Manning's book R in Action, Third Edition.
You can see an example of R’s graphic capabilities in figure 1.2. This graph describes the relationships between years of experience and wages for men in women in six industries, collected from the US Current Population Survey in 1985. Technically, it’s a matrix of scatterplots with gender displayed by color and symbol. Trends are described using linear regression lines. If these terms scatterplot and regression lines are unfamiliar to you, don’t worry. We’ll cover them in later chapters.
A matrix is a two-dimensional array in which each element has the same mode (numeric, character, or logical). Matrices are created with the matrix function. The general format is
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns, byrow=logical_value, dimnames=list( char_vector_rownames, char_vector_colnames))where vector contains the elements for the matrix, nrow and ncol specify the row and column dimensions, and dimnames contains optional row and column labels stored in character vectors. The option byrow indicates whether the matrix should be filled in by row (byrow=TRUE) or by column (byrow=FALSE). The default is by column. The following listing demonstrates the matrix function.
Listing 2.1 Creating matrices
> y <- matrix(1:20, nrow=5, ncol=4) #1 > y [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20 > cells <- c(1,26,24,68) > rnames <- c("R1", "R2") > cnames <- c("C1", "C2") #2 > mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames)) > mymatrix C1 C2 R1 1 26 R2 24 68 > mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=FALSE, dimnames=list(rnames, cnames)) > mymatrix #3 C1 C2 R1 1 24 R2 26 68First you create a 5 ´ 4 matrix #1. Then you create a 2 ´ 2 matrix with labels and fill the matrix by rows #2. Finally, you create a 2 ´ 2 matrix and fill the matrix by columns #3.
You can identify rows, columns, or elements of a matrix by using subscripts and brackets. X[i,] refers to the ith row of matrix X, X[,j] refers to the jth column, and X[i, j] refers to the ijth element, respectively. The subscripts i and j can be numeric vectors in order to select multiple rows or columns, as shown in the following listing.

This is an excerpt from Manning's book Beyond Spreadsheets with R.
But this is prone to losing track of which values correspond to which. Instead, you can create a matrix of values — a two-dimensional grid of rows and columns. The function
matrix()takes a data vector as its first argument, along with a specification of the number of rows and columns in which to store this data. You can store thesexandyvalues by first combining them into a larger vector withc()and then specifying that you want to store these as a matrix with four rows (one for each pair of coordinates) and two columns (one for each ofxandy):xy <- matrix(data = c(x, y), nrow = 4, ncol = 2) xy #> [,1] [,2] #> [1,] 0.26 0.01 #> [2,] 0.57 0.58 #> [3,] 0.84 0.82 #> [4,] 0.72 0.96The
matrix()function will populate this matrix one column at a time by default. If the data is arranged such that it needs to be entered one row at a time, there is an optional argumentbyrow. Note that because the input is a vector (created usingc()here), the elements must all be of the same type. This is a restriction on thematrixobject.
Figure 5.5 A table of values in a spreadsheet, similar to a matrix
![]()
The
str()function applied to a matrix tells you about each of the dimensions (note that still only a single type is reported, because all elements must be of this type):str( matrix(1:8, nrow = 2) ) #> int [1:2, 1:4] 1 2 3 4 5 6 7 8These dimensions are also recorded in an attribute of the matrix with the label
dim

This is an excerpt from Manning's book R in Action, Second Edition: Data analysis and graphics with R.
R is a case-sensitive, interpreted language. You can enter commands one at a time at the command prompt (>) or run a set of commands from a source file. There are a wide variety of data types, including vectors, matrices, data frames (similar to datasets), and lists (collections of objects). We’ll discuss each of these data types in chapter 2.
A matrix is a two-dimensional array in which each element has the same mode (numeric, character, or logical). Matrices are created with the matrix() function. The general format is
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns, byrow=logical_value, dimnames=list( char_vector_rownames, char_vector_colnames))where vector contains the elements for the matrix, nrow and ncol specify the row and column dimensions, and dimnames contains optional row and column labels stored in character vectors. The option byrow indicates whether the matrix should be filled in by row (byrow=TRUE) or by column (byrow=FALSE). The default is by column. The following listing demonstrates the matrix function.
Listing 2.1. Creating matrices
> y <- matrix(1:20, nrow=5, ncol=4) #1 > y [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 2 0 > cells <- c(1,26,24,68) > rnames <- c("R1", "R2") > cnames <- c("C1", "C2") > mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, #2 dimnames=list(rnames, cnames)) #2 > mymatrix C1 C2 R1 1 26 R2 24 68 > mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=FALSE, #3 dimnames=list(rnames, cnames)) #3 > mymatrix C1 C2 R1 1 24 R2 26 68 #1: 1 Creates a 5 x 4 matrix #2: 2 2 x 2 matrix filled by rows #3: 3 2 x 2 matrix filled by columnsFirst you create a 5 × 4 matrix
. Then you create a 2 × 2 matrix with labels and fill the matrix by rows
. Finally, you create a 2 × 2 matrix and fill the matrix by columns
.
You can identify rows, columns, or elements of a matrix by using subscripts and brackets. X[i,] refers to the ith row of matrix X, X[,j] refers to the jth column, and X[i, j] refers to the ijth element, respectively. The subscripts i and j can be numeric vectors in order to select multiple rows or columns, as shown in the following listing.
. Then you create a 2 × 2 matrix with labels and fill the matrix by rows
. Finally, you create a 2 × 2 matrix and fill the matrix by columns
.