next up previous contents
Next: Graphics Up: Splus/R Previous: Subsetting Matrices   Contents

Object Arithmetic

Simple, arithmetic expressions operate elementwise on vectors and matrices. For instance, suppose we want to multiply the vector xx with a sequence from 1 to 6, defined by 1:6. Doing this in R will produce

> xx
[1] 1 2 3 1 2 3
> 1:6
[1] 1 2 3 4 5 6
> xx*1:6
[1]  1  4  9  4 10 18
so that R is literally producing the new vector by xx[i]*1:6[i] for i=$ 1,\dots,6$. The same is true for matrices so that if new.m <- tm*mm where tm and mm are defined above, new.m[i,j] = tm[i,j]*mm[i,j]. The point is that S and R are not multplying vectors and matrices in the usual mathematical sense. You can, however, do matrix multiplication in both laguages. For instance,
> mm%*%t(mm)
     [,1] [,2] [,3]
[1,]   66   78   90
[2,]   78   93  108
[3,]   90  108  126
will yield mm multiplied by the transpose of mm.



Computer Support Group 2002-10-07