The Art of R Programming

(WallPaper) #1
>m
[,1] [,2]
[1,] 1 2
[2,] 7 8
> dm <- diag(m)
>dm
[1]18
> diag(dm)
[,1] [,2]
[1,] 1 0
[2,] 0 8
> diag(3)
[,1] [,2] [,3]
[1,]100
[2,]010
[3,]001

Thesweep()function is capable of fairly complex operations. As a simple
example, let’s take a 3-by-3 matrix and add 1 to row 1, 4 to row 2, and 7 to
row 3.

>m
[,1] [,2] [,3]
[1,]123
[2,]456
[3,]789
> sweep(m,1,c(1,4,7),"+")
[,1] [,2] [,3]
[1,]234
[2,] 8 9 10
[3,] 14 15 16

The first two arguments tosweep()are like those ofapply(): the array
and the margin, which is 1 for rows in this case. The fourth argument is a
function to be applied, and the third is an argument to that function (to the
"+"function).

8.4.1 Extended Example: Vector Cross Product..........................


Let’s consider the issue of vector cross products. The definition is very
simple: The cross product of vectors(x 1 ,x 2 ,x 3 )and(y 1 ,y 2 ,y 3 )in three-
dimensional space is a new three-dimensional vector, as shown in Equa-
tion 8.1.

(x 2 y 3 −x 3 y 2 ,−x 1 y 3 +x 3 y 1 ,x 1 y 2 −x 2 y 1 ) (8.1)

198 Chapter 8

Free download pdf