MATRI
CESI
NR
Matri
cesaretheRobj ectsi
nwhi chtheel
ementsarearr
angedinatwo-
di
mensionalrectangularlayout
.Theycontai
nelementsoft
hesame
at
[Link] r
icesthatcontai
nnumericel
ementsareusedin
mathemati
cal calculat
ions.
AMat
ri
xiscr
eat
edi
nRusi
ngt
hemat
ri
x()f
unct
ion.
TheRcodef
orcr
eat
ingamat
ri
xis;
mat
ri
x(dat
a,nr
ow,
ncol
,by
row,
dimnames)
l
et’
sknowt
hedescr
ipt
ionoft
hepar
amet
ers
dataistheinputnumber s(
vect
or)whichareelementsoft
hematri
x
nrowisthenumberofr owsofthemat r
ix
ncoli
sthenumberofcol umnsofthemat ri
x
byrowislogical
.Ifbyr
owi sTRUE,thentheinputvect
orel
ement
s
arearr
angedi nrow-wise
di
mnamei sthenamesassi gnedtorowsandcol umns
Exampl
e
A=mat
ri
x(c(
3:14)
,nr
ow=4,
byr
ow=TRUE)#her
eel
ement
sar
ear
ranged
sequenti
all
ybyrow
A
[,
1][,
2][,
3]
[1,
]3 4 5
[2,
]6 7 8
[3,
] 9 10 11
[4,
]12 13 14
B=matri
x(c(3:
14),
nrow=4,
byr
ow=FALSE)#her
eel
ement
sar
ear
ranged
sequenti
all
ybycol
umn
B
[,
1][,
2][,
3]
[1,
] 3 7 11
[2,
] 4 8 12
[3,
] 5 9 13
[4,
] 6 10 14
Assignthecol
umnandr ownames.
rownames=c( "r
ow1", "
row2","r
ow3","
row4")#namesoftherows
colnames=c("col1"
,"col2"
,"col
3")#namesofcolumns
A=mat r
ix(
c(3:
14),nr
ow=4, byrow=TRUE, di
mnames=list(
rownames,
c
olnames))
A
col
1col2col3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14
Accessi
ngEl
ement
sofaMat
ri
x
Wecanaccessel
ement
sofamat
ri
xusi
ngt
hesquar
ebr
acket[
]indexi
ng
met
hod.
Exampl
es
rownames=c( "r
ow1" ,"
row2","r
ow3", "
row4")
colnames=c( "col1","
col2",
"col
3")
A=mat r
ix(
c(3:
14) ,nrow=4, byrow=TRUE, di
mnames=l
ist
(rownames,
col
names) )
A
col1col2col 3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14
A[1,3]#accesst heelementatt he1strow3r dcol
umn
[1]5
A[1,]#accessal l theel
ement sinrow1
col1col 2col3
3 4 5
A[,1]#accessal l theel
ement sincolumn1
row1r ow2r ow3r ow4
3 6 9 12
A[c(1,2),
c(2,
3)]#sel ectsrow1&2andcol umn2&3
col2col3
row1 4 5
row2 7 8
A[c(1,
2),
]#select
sr ow1andr ow2
col
1col2col3
row1 3 4 5
row2 6 7 8
A[,
c(2,
3)]#select
scol umn2andcolumn3
col
2col3
row1 4 5
row2 7 8
row3 10 11
row4 13 14
A[-1,
]#select
sallrowsexceptrow1
col
1col2col3
row2 6 7 8
row3 9 10 11
row4 12 13 14
A[,
-3]#select
sallcolumnsexceptcol
umn3
col
1col2
row1 3 4
row2 6 7
row3 9 10
row4 12 13
Mat
ri
xComput
ati
oni
nR
Vari
ousmathemat i
caloperat
ionsareperfor
medonthemat r
icesusing
theRoperat
[Link]
ionisal
soamatr
[Link]
dimensi
ons(numberofr owsandcol umns)shoul
dbesamef orthe
matri
cesinv
olvedintheoperation.
Addi
ti
onandsubt
ract
ionofmat
ri
ces
Exampl
e
A=matr
ix(
c(2,
3,
4,
1,
3,
5),
nrow=2,
byr
ow=TRUE)#a2x3mat
ri
x
A
[
,1][
,
2][,
3]
[
1,]2 3 4
[
2,]1 3 5
B=matr
ix(
c(3,
1,
3,
4,
2,
1),
nrow=2,
byr
ow=TRUE)#a2x3mat
ri
x
B
[
,1][
,
2][,
3]
[
1,]3 1 3
[
2,]4 2 1
A+B
[
,1][
,
2][
,
3]
[
1,]5 4 7
[
2,]5 5 6
A-
B
[
,1][
,
2][
,
3]
[
1,]-1 2 1
[
2,]-3 1 4
Mul
ti
pli
cat
ionofmat
ri
ces
Theoper
ator%*
%isusedf
ormat
ri
xmul
ti
pli
cat
ioni
nR
A=mat
ri
x(c(
3,9,
-
1,4)
,nr
ow=2,
byr
ow=TRUE)#a2x2mat
ri
x
A
[
,1][
,
2]
[
1,]3 9
[
2,]-1 4
B=matr
ix(
c(6,
3,
1,
8),
nrow=2,
byr
ow=TRUE)#a2x2mat
ri
x
B
[
,1][
,
2]
[
1,]6 3
[
2,]1 8
A%*%B#AB
[
,1][
,
2]
[
1,]27 81
[
2,]-2 29
B%*%A#BA
[
,1][
,
2]
[
1,]15 66
[
2,]-5 41
Matrixoperati
onsinR
Rhassomei n-bui
l
dfunct
ionst
hatal
lowy outoperfor
m somemat ri
x
operati
onssuchasf i
ndi
ngthedet
erminant,t
ranspose,ei
genv
aluesand
ei
genv ect
orsetc.
Let’
sgetstarted.
t
():
ret
urnst
het
ransposeofamat
ri
x
A=matr
ix(
c(3,
9,
-1,
4),
nrow=2,
byr
ow=TRUE)#a2x2mat
ri
x
A
[,
1][
,
2]
[
1,]3 9
[
2,]-1 4
t(
A)
[,
1][
,
2]
[
1,]3- 1
[
2,]9 4
det
():r
eturnsthedetermi
nantofamatr
ix
A=matri
x(c(3,
9,-
1,4)
,nr
ow=2,byrow=TRUE)#a2x2mat
ri
x
A
[
,1][
,2]
[
1,]3 9
[
2,]-1 4
det
(A)
[
1]21
di
ag():
ret
urnsthediagonal
element
sofamatr
ix
A=matri
x(c(
3,9,
-
1,4)
,nrow=2,byr
ow=TRUE)#a2x2mat
ri
x
A
[
,1][
,2]
[
1,]3 9
[
2,]-1 4
di
ag(A)
[
1]34
sol
ve()
:returnsthei
nverseofamatr
ix
A=matri
x(c(3,9,
-
1,4)
,nr
ow=2, by
row=TRUE)#a2x2mat
ri
x
A
[
,1][
,2]
[
1,]3 9
[
2,]-
1 4
solv
e( A)
[,
1] [ ,
2]
[1,
]0.19047619-0.
4285714
[2,
]0.047619050.1428571
eigen():
Computeseigenv
aluesandei
genv
ector
sofamatr
ix
B=mat ri
x(c(
6,3,
1,
8),
nrow=2,byrow=TRUE)#a2x2matr
ix
B
[
,1][,
2]
[1,
]6 3
[2,
]1 8
eigen(B)
eigen()decomposit
ion
$values
[1]95
$vect
ors
[,
1] [ ,
2]
[
1,]-
0.7071068-0.
9486833
[
2,]-
0.70710680.3162278
sum( diag(B)):r
eturnst hetr
aceoft hemat
ri
xB
B=mat rix(
c(6,3,
1,8),
nrow=2, byrow=TRUE)#a2x2matr
ix
B
[
,1][,
2]
[1,
]6 3
[2,
]1 8
sum( diag(B))
[1]14
qr(
A)$rank: r
eturnsther ankofmat r
ixA
A=mat rix(
c(3,9,
-1,4)
,nrow=2, by
r ow=TRUE)#a2x2mat
ri
x
A
[
,1][,
2]
[1,
]3 9
[2,
]- 1 4
qr(A)$rank
[1]2
di
m()
:ret
urnst
hedi
mensi
onofamat
ri
x
A=mat
ri
x(c(
3,9,
-
1,4)
,nr
ow=2,
byr
ow=TRUE)#a2x2mat
ri
x
A
[
,1][
,2]
[
1,]3 9
[
2,]-1 4
di
m( A)
[
1]22
colSums():computesthesumsoft
hecol
umnsofamat
ri
x
A=mat r
ix(
c(3,9,
-
1,4)
,nr
ow=2,byr
ow=TRUE)#a2x2matr
ix
A
[
,1][
,
2]
[1,
]3 9
[2,
]- 1 4
colSums(A)
[1]213
rowSums():
computesthesumsoft
herowsofamat
ri
x
A=matri
x(c(
3,9,
-
1,4)
,nr
ow=2,by
row=TRUE)#a2x2matr
ix
A
[,
1][
,2]
[
1,]3 9
[
2,]-1 4
rowSums(A)
[
1]123
colMeans( ):computesthemeansoft
hecol
umnsofamatr
ix
A=mat ri
x(c(3,9,
-
1,4)
,nr
ow=2,byrow=TRUE)#a2x2mat
ri
x
A
[
,1][
,2]
[1,
]3 9
[2,
]- 1 4
colMeans( A)
[1]1.06.5
rowMeans(
):comput esthemeansoft
her
owsofamatr
ix
A=matr
ix(
c(3,
9,-
1,4)
,nrow=2,byr
ow=TRUE)#a2x2matr
ix
A
[,
1][
,
2]
[
1,]3 9
[
2,]-
1 4
r
owMeans(
A)
[
1]6.
01.
5
di
ag(1,
3):
creat
esa3x3identi
tymat
ri
x
di
ag(1,
3)#put1onthediagonal
sofa3x3mat
ri
x
[
,1][
,
2][,
3]
[
1,]1 0 0
[
2,]0 1 0
[
3,]0 0 1
di
ag(2,
3)#put2ont
hedi
agonal
sofa3x3mat
ri
x
[
,1][
,
2][,
3]
[
1,]2 0 0
[
2,]0 2 0
[
3,]0 0 2
Solv
ingSi
mult
aneousEquat
ionsi
nR
Supposewehavet
hefol
lowi
ngequat
ions:
2x+y=12
6x+5y=40
Theabov eequat i
onsarewrit
tenintheform ofAx=b,wher [ ]
eA=
2 1
6 5
,
b=[]12
40 []
andx= y
x
Wecansol vef orxusingt
hesol ve(
A,b)f
unctioni
nR.
Rcode
A=matrix(
c(2,1,6,
5),
nrow=2,byrow=TRUE)#mat r
ixA
A
[,
1][,
2]
[1,
]2 1
[2,
]6 5
b=matri
x (
c(12,40),
nrow=2,byrow=TRUE)#mat r
ixb
b
[,
1]
[1,
]12
[2,
]40
sol
ve(A,b)
[,
1]
[1,
]5
[2,
]2
OR
l
m( b~.+0,
data=dat
a.f
rame(x=c(2,6)
,
y=c(1,5),
b=c(12,40))
)
Call
:
l
m( f
ormul
a=b~.+0,dat
a=dat
a.f
rame(
x=c(
2,6)
,y=c(
1,
5)
,b=c(12,
40)
))
Coef
fi
cient
s:
xy
52
RcodeforTri
gonometrici
nDegrees
Bydefaul
tthetr
igonometr
icf
uncti
onsi
nRcomputet
heanglesinr
adi
an
butwecandot hedegreet
oradi
anconver
ti
onusi
ngt
hefol
lowing:
cos(x*(
pi/
180) )
:ret
urnsthecosi
neofangl
exi
ndegr
ees
cos(90*(
pi/180))#cos(90)i
ndegrees
[1]6.
123032e-17
cos(30*(
pi/180))#cos(30)i
ndegrees
[1]0.
8660254
sin(x*(
pi/
180) )
:ret
urnsthesi
neofangl
exi
ndegr
ees
sin(90*(
pi/180))#sin(
90)indegr
ees
[1]1
sin(30*(
pi/180))#sin(
30)indegr
ees
[1]0.5
t
an(x*(pi/180)):
r et
urnsthetangentofangl
exi
ndegr
ees
tan(45*(pi/180))#tan(45)i
ndegr ees
[
1]1
t
an(60* (
pi/180))#t an(
60)indegrees
[
1]1.732051
Ant i
logi nR
Ther ei snoknowni n-bui
l
tfunctioninRforant
il
ogcal
cul
ati
onbutwecan
alway smakeuseoft her ai
set opoweroperat
orinR.
Exampl es
3^log( 9,3)#log9base3
[1]9
#OR
3^2#si ncel og(9,
3)=2
[1]9
l
og10( 5)#l ogtothebase10
[1]0.69897
10^ l
og10( 5)
[1]5
#OR
10^ 0.69897#si ncelog10(5)=0.69897
[1]5
Pi
echart
Apie-
charti
sar epr
esent
ati
onofval
uesassli
cesofaci
rcl
ewi t
h
di
ff
erentcol
[Link]
echarti
screat
edusingt
hepi
e()functi
on
pi
e(x,
label
s,
mai
n,col
,
)
Fol
l
owi
ngi
sthedescr
ipt
ionoft
hepar
amet
ersused−
xisav ectorcontai
ningthenumeri
cv al
uesusedi
nthepi
echar
t.
labelsisusedt ogivedescripti
ont
othesli
ces.
mainindicatestheti
tleofthechar
t.
coli
ndicatesthecolorpalette.
Exampl e
Av erysimplepie-char
tiscreat
edusingjustt
hei
nputv
ect
orandl
abel
s.
x=c( 90,60,180,200)
l
abel s=c("Togo"
, "
Niger
ia"
,"Mali
","
Ghana")
pi
e( x,
label
s)#Pl otthechar
t
Piechartwi thtit
leandcol our
Wecanexpandt hefeaturesoft hechartbyaddingmoreparameterst o
thefuncti
[Link] llusepar ametermaintoaddat it
letothechartand
anotherparamet eriscol whichwillmakeuseofr ai
nbowcolourpallet
whil
edr awingt [Link] engt
hofthepalletshoul
dbesameast he
numberofv alueswehav ef orthechart
.Henceweusel engt
h(x)
.
Example
x=c(90,60,180,200)
l
abels=c("Togo"
,"Nigeri
a",
"Mal i
","
Ghana")
pi
e(x,l
abels,mai
n=" Count
riesandt hei
rtot
alconsumpt
ions"
,col=r
ainb
ow(l
ength(x))
)#Plotthechar twi
thtitl
eandrai
nbowcolorpal
let
.
Sl
icePer
cent
agesandChar
tLegend
Wecanaddsliceper
cent
ageandachar
tlegendbycr
eat
ingaddi
ti
onal
char
tvar
iabl
es
X=c(
90,60,180,
200)
l
abel
s=c("
Togo","
Niger
ia"
,"Mal
i
","
Ghana"
)
pi
eper
cent
=round(
100*
x/sum(
x),
1)#per
cent
ageconsumpt
ions
pi
e(x,
label
s=piepercent,mai
n="Count
ri
esandt
hei
rtot
alconsumpt
ion
s"
,col
=r ai
nbow(
length(x)
))#pl
ott
hechart
l
egend("
toplef
t",
c("Togo"
,"
Niger
ia"
,"
Mal
i
","
Ghana"
),cex=0.
8,f
il
l=r
ainbow
(
lengt
h(x))
)#addl egendtothechar
t
sasdf