Thus the expressionrow(m) == col(m)in the same line returns a matrix
ofTRUEandFALSEvalues,TRUEvalues on the diagonal of the matrix andFALSE
values elsewhere. Once again, keep in mind that binary operators—in this
case,==—are functions. Of course,row()andcol()are functions too, so this
expression:
row(m) == col(m)
applies that function to each element of the matrixm, and it returns a
TRUE/FALSEmatrix of the same size asm. Theifelse()expression is another
function call.
ifelse(row(m) == col(m),1,rho)
In this case, with the argument being theTRUE/FALSEmatrix just discussed,
the result is to place the values 1 andrhoin the proper places in our output
matrix.
3.3 Applying Functions to Matrix Rows and Columns..............................
One of the most famous and most used features of R is the*apply()family of
functions, such asapply(),tapply(), andlapply(). Here, we’ll look atapply(),
which instructs R to call a user-specified function on each of the rows or
each of the columns of a matrix.
3.3.1 Using the apply() Function.......................................
This is the general form ofapplyfor matrices:
apply(m,dimcode,f,fargs)
where the arguments are as follows:
- mis the matrix.
- dimcodeis the dimension, equal to 1 if the function applies to rows or 2
for columns. - fis the function to be applied.
- fargsis an optional set of arguments to be supplied tof.
For example, here we apply the R functionmean()to each column of a
matrixz:
>z
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
70 Chapter 3