## Multivariate Statistical Techniques Vector Operations in R

R is an open-source Statistical that is rich in vector and mtrix operators. There are versions of R available for Windows, Mac OS and Unix that can be freely downloaded over the Internet.

Note: By default, R displays the elements of an array as a series of horizontal values. This is different from what we are used to in the lectures and in some of the other packages. Not to worry, all of the functions work in the correct manner.

#### The Vector

```> v <- c(3,2,2)
> v

 3 2 2```

#### Is Something A Vector

```> is.vector(v)

 TRUE

> is.matrix(v)

 FALSE```

#### "Transpose" of a Vector

```> vp <- t(v)
> vp

[,1] [,2] [,3]
[1,]    3    2    2```

```> w <- c(4,1,2)
> s <- v + w
> s

 7 3 4```

#### Subtracting Vectors

```> d <- v - w
> d

 -1  1  0```

#### Multiplication by a Scalar

```> s <- 3*v
> s

 9 6 6

> t <- v*3
> t

 9 6 6```

#### Vector Dot Product

```> d <- v %*% w
> d

[,1]
[1,]   18

> d <- w %*% v
> d

[,1]
[1,]   18

> d <- v %*% v
> d

[,1]
[1,]   17

> d <- w %*% w
> d

[,1]
[1,]   21```

#### Length of a Vector

```> a <- c(4,3)
> a

 4 3

> sqrt(a %*% a)

[,1]
[1,]    5

> v

 3 2 2

> sqrt(v %*% v)

[,1]
[1,] 4.123106

> w

 4 1 2

> sqrt(w %*% w)

[,1]
[1,] 4.582576

# you can write a function to get the norm of a vector
> normvec<-function(x)
+ {sqrt(x%*%x)}

> normvec(v)

[,1]
[1,] 4.582576```

#### Unit Length Vector

```> l <- c(1/2 , 1/2 , 1/2 , 1/2)
> l

 0.5 0.5 0.5 0.5

> sqrt(l %*% l)

[,1]
[1,]    1```

#### The Unit Vector

```> u <- c(1,1,1)
> u

 1 1 1

> u %*% u

[,1]
[1,]    3

> v

 3 2 2

> u %*% v

[,1]
[1,]    7

> w

 4 1 2

> u %*% w

[,1]
[1,]    7```

Phil Ender, 23feb05