0% found this document useful (0 votes)
12 views8 pages

Working With Vectors and Matrices

Uploaded by

Vineeta D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Working With Vectors and Matrices

Uploaded by

Vineeta D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Working with Vectors and Matrices

Constructing vectors

I Integers from 9 to 17
> x<-9:17
> x
[1] 9 10 11 12 13 14 15 16 17
I A sequence of 11 numbers from 0 to 1
> y<-seq(0,1,length=11)
> y
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
I The same number or the same vector several times
> z<-rep(1:2, 5)
> z
[1] 1 2 1 2 1 2 1 2 1 2
I Combine numbers, vectors or both into a new vector
> xz10<-c(x,z,10)
> xz10
[1] 9 10 11 12 13 14 15 16 17 1 2 1 2 1 2 1 2 1 2 10
Constructing matrices

I Combine rows into a matrix


> A<-rbind(1:3, c(1,1,2))
> A
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 1 2
I Or columns
> B<-cbind(1:3, c(1,1,2))
> B
[,1] [,2]
[1,] 1 1
[2,] 2 1
[3,] 3 2
I Define a matrix from one long vector
> C<-matrix(c(1,0,0,1,1,0,1,1,1), nrow=3, ncol=3)
> C
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 0 1 1
[3,] 0 0 1
Can also be done by rows by adding “, byrow=TRUE” before the last
parenthesis
Index and logical index

I Important for optimal use of R


I Example: Define a vector with integers from (-5) to 5 and extract the
numbers with absolute value less than 3.
> x<- (-5):5
> x
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5
I by their index in the vector
> x[4:8]
[1] -2 -1 0 1 2
I by negative selection (set a minus in front of the indices we don’t want)
> x[-c(1:3,9:11)]
[1] -2 -1 0 1 2
I A logical vector can be defined by
> index<-abs(x)<3
> index
[1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
I Now this vector can be used to extract the wanted numbers
> x[index]
[1] -2 -1 0 1 2
Index and logical index
I This also works for matrices:
> A<-matrix((-4):5, nrow=2, ncol=5)
> A
[,1] [,2] [,3] [,4] [,5]
[1,] -4 -2 0 2 4
[2,] -3 -1 1 3 5
> A[A<0]
[1] -4 -3 -2 -1
I And for assignments
> A[A<0]<-0
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 2 4
[2,] 0 0 1 3 5
I Matrix rows can be selected by
> A[2,]
[1] 0 0 1 3 5
I and similarly for columns
> A[,c(2,4)]
[,1] [,2]
[1,] 0 2
[2,] 0 3
Properties of vectors and matrices

The mode of the vector or matrix detects the type of singles that is stored:

> A<-matrix(rep(c(TRUE,FALSE),2),nrow=2)
> B<-rnorm(4)
> C<-matrix(LETTERS[1:9],nrow=3)
> A;B;C
[,1] [,2]
[1,] TRUE TRUE
[2,] FALSE FALSE
[1] 0.6613129 0.8583421 1.2516685 -1.2147030
[,1] [,2] [,3]
[1,] "A" "D" "G"
[2,] "B" "E" "H"
[3,] "C" "F" "I"
> mode(A);mode(B);mode(C)
[1] "logical"
[1] "numeric"
[1] "character"
Properties of vectors and matrices

Vectors and matrices have lengths: The length is the number of elements:

> x<-matrix(c(NA,2:12),ncol=3)
> x
[,1] [,2] [,3]
[1,] NA 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> length(x[1,])
[1] 3
> length(x)
[1] 12

The dimension of a matrix is the number of rows and columns: The number of
columns is the second element.

> dim(x); dim(x)[2]


[1] 4 3
[1] 3
Naming rows and columns in a matrix

We can add names to a matrix with the dimnames() function:


> x<-matrix(rnorm(12),nrow=4)
> x
[,1] [,2] [,3]
[1,] 1.5231529 -0.5784787 -0.7314045
[2,] -1.2359829 0.2717685 -0.8343691
[3,] 1.6658531 -0.3012827 0.6278903
[4,] -0.5035843 -0.4158793 0.7975808
> dimnames(x)[[2]]<-paste("data",1:3,sep="")
> dimnames(x)[[1]]<-paste("obs",1:4,sep="")
> x
data1 data2 data3
obs1 1.5231529 -0.5784787 -0.7314045
obs2 -1.2359829 0.2717685 -0.8343691
obs3 1.6658531 -0.3012827 0.6278903
obs4 -0.5035843 -0.4158793 0.7975808

Retrieval:
> rownames(x)
[1] "obs1" "obs2" "obs3" "obs4"
> colnames(x)
[1] "data1" "data2" "data3"

You might also like