You are on page 1of 70

#=============================================================================

====
Basi
csofRandDat aTy pes
#=============================================================================
====
#ff6600
#ff0000
#fb641b

Ri
nAct
ion

Hands-
onPr
ogr
ammi
ngwi
thR

Rf
orEv
ery
one:
Adv
ancedAnal
yti
csandGr
aphi
cs

RCookbook

Machi
neLear
ningwi
thR

Pr
act
ical
Dat
aSci
encewi
thR

I
ntr
oduct
iont
oSt
ati
sti
cal
Lear
ning

#Exampl e-6(SecondMaxi mum Number)


vec<-c(1,8,
6,4,
5)
second.max<-f uncti
on(vec){
secondLargestNum <-0
l
argestNum <-0
for
(num inv ec){

i
f(l
argestNum <num ){
secondLar
gestNum =l
argestNum;
l
argestNum =num;
}
i
f(secondLargest
Num <num &&largestNum !
=num )
secondLar
gestNum =num;

}
r
etur
n(secondLar
gest
Num)
}

#Ev al
uatet
hefunct
ionwi
thar
gument
s
second.max(v
ec)

i
f( scor
e==100){
grade=’A’ ;
cout<<" Superb"<<endl;
}
el
sei f(
scor e>=90){
grade=’A’ ;
cout<<" Excel
lent"<<endl
;
}
el
sei f(
scor e>=80){
grade=’B’;
cout<<" VeryGood"<<endl;
}
el
seif(score>=70){
grade=’C’;
cout<<"Good"<<endl
;
}
el
seif(score>=60)
grade=’D’;
el
se
grade=’F’
;

i
f( (
Ch>=’a’
)&&( Ch<=’z’
)){
//codetoprocessl
ower -
caseleter
}
elseif(
(Ch>=’A’
)&&(Ch<=’ Z’)
){
//codetoprocessupper-
caseleter
}
elseif(
(Ch>=’0’
)&&(Ch<=’ 9’)
){
//codetoprocessdi
gitcharacter
}
else{
//codetoprocessnon-l
ett
er/non-di
gitchar
act
ers
}

Assi gnav al
uetodoubl evari
ablecostdependi
ngont
hev
alueofi
ntegerv
ari
abl
e
distanceasf ol
lows:
DistanceCost
--
---
----
--
--
--
--
--
--
--
--
---
--
---
---
--
--
--
--
-
0t hrough1005. 00
Mor et han100butnotmor ethan5008.00
Mor et han500butl essthan1,00010.00
1,
000ormor e12.00

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
# RBasicSyntax&DataTy pes
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--

1.PRINTHELLOWORLD:
pri
nt(
"Hel
l
oWor l
d")

2.ADDTWONUMBERS:
pri
nt(
23.
9+11.
6)

3.RScr
iptFi
l
e/Comment
s(Myf
ir
stpr
ogr
am i
nRPr
ogr
ammi
ng)

i
f(
FALSE) {
"
Thisisademof ormul t
i-
li
necomment
sandi
tshoul
dbeput
i
nsi
deei therasi
ngl
eofdoublequot
e"
}
myStri
ng<-"Hel l
o,Worl
d!"
pr
int(my Str
ing)

4.RWORKASACALCULATOR:

1+2 #Addi
ti
on
5-
2 #Subtr
acti
on
2*
3 #Mul ti
pli
cati
on
3/
2 #Divisi
on
2*
*3#Exponentati
on
2^
3 #Exponent at
ion

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
# WorkSpace
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--

5.REGARDI
NGWORKSPACE:

getwd( )#Getwor ki
ngdi r
ectory
set
wd( "
C:\\Users\\
Anup\ \Deskt
op\ \
RFiles"
)#Set
ti
nganewwor
kingdi
rect
ory
x<-
10
pri
nt(x)
X<-20
pri
nt(X)
l
s()#Li stsobjectsi
ncur r
entwor kspace
rm("x")#Remov eobjectxfrom workspace
rm(li
st=ls()
)#Remov eallobject
sinwor kspace

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
# Creat
eVariabl
es
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--

6.HOW TOCREATEAVARI
ABLE:

x<-
10
pri
nt(x)
X<-20
pri
nt(X) #Riscasesensi
ti
ve
assign(
"x"
,15)
assign(
"X"
,25)#Alt
ernat
ivewayofcr
eat
ingv
ari
abl
ebutnotpr
efer
red

7.NAMI
NGCONVENTI
ONS(CORRECTWAYS)

student
Names<-c("Anup",
"Sangram",
"Subhaji
t"
)
student
.names<-c(
"Anup",
"Sangram",
"Subhaji
t"
)#Bestway
student
_names<-c("
Anup",
"Sangr am"
,"
Subhajit
")
student
names3<-c("Anup"
,"Sangram"
,"Subhaj
it
")
.st
udentnames<-c(
"Anup",
"Sangram",
"Subhaji
t"
)#Correctbutnotpr
efer
red

8.NAMI
NGCONVENTI
ONS(I
NCORRECTWAYS)

3studentnames<-c(
"Anup",
"Sangram","Subhaji
t"
)
studentnames<-c("
Anup","
Sangram" ,
"Subhajit
")
student@names<-c("Anup"
,"Sangram","Subhaj
it
")
student!
names<-c("
Anup","
Sangram" ,
"Subhajit
")

9.?
Reser
ved #Cannotuser
eser
vedwor
dsasv
ari
abl
enames

10.QUI
TTI
NGR
q()

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
# Gett
ingHelp
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

st
udent.
mar ks<-c(
30, 60,
90)
st
udent.
mar ks
mean(st
udent .
mar ks) #Ar ithmet
icmean
medi
an(student.mar ks)#Ar
ithmeti
cmedian

hel
p.star
t()
?mean #i
fweknowthefuncti
on
hel
p(mean)#Sameas? mean
??mean #i
fyoudonotknowthefunct
ion
??"
ari
thmet i
cmean"
hel
p.search(
"ar
it
hmeti
cmean")#sameas? ?"
ari
thmet
icmean"

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
# Packages
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

l
ibrary(
)#Al li
nstal
ledpackages
search()#Allloadedpackages
i
nstall
.packages("tm",
dependenci
es=TRUE)#I
nst
all
package"
tm"
l
ibrary(
tm)#Loadpackage" tm"
detach("package:t
m")#Unloadpackage

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
# Si
mpleDat aTypes
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

1.LOGI
CAL(TRUE,
FALSE)

v<-TRUE
pr
int
(cl
ass(
v))

2.NUMERI
C(12.
3,5,
999)

v<-23.
5
pr
int
(cl
ass(
v))

3.I
NTEGER(2L,
34L,
0L)

v<-2L
pr
int
(cl
ass(
v))

4.COMPLEX(3+2i
)

v<-2+5i
pr
int
(cl
ass(
v))

5.CHARACTER('
A',
"GOOD"
,"TRUE"
,'
23.
4')

v<-"
TRUE"
pr
int
(cl
ass(
v))

6.RAW ("
HELLO"I
SSTOREDAS48656C6C6F)
v<-charToRaw("
Hel
l
o")
pr
int
(class(
v))

==============================================================================
====
Rdatastruct
ures

Therearenumerousty
pesofdatastruct
uresacrosspr ogrammingl
anguages,each
wit
hst r
engthsandweaknessessui
tedtopart
iculartasks.Si
nceRisaprogrammi ng
l
anguageusedwi del
yforstati
sti
caldat
aanal ysis,t
hedat astr
uct
uresituti
l
izesweredesi
gned
wit
ht hi
stypeofworki
nmi nd.

TheRdat astr
ucturesusedmostfrequentl
yinmachinelear
ningarevector
s,
factor
s,li
sts,arrays and mat
ri
ces,and dat aframes.Each i stail
ored to a speci
fi
c data
managementt ask,whichmakesi
timpor t
anttounderst
andhowt heywilli
nter
acti
ny ourRproj
ect.
I
nt hesecti
onsthatfol
low,wewil
lrevi
ewt hei
rsi
milari
ti
esanddiff
erences.

1.Vect
ors(Avect
orcancont
ainanynumberofelements,butal
loftheel
ements
mustbeofthesametypeofval
ues.Fori
nstance,
av ect
orcannotcont
ainbot
hnumber
sandt
ext
.
Todeter
minethet
ypeofvect
orv,usethety
peof(v)command.)

subject_name <-c( "Mathemat ics"


,"St
ati
sti
c","Machi
ne Lear
ning"
,"
Dat
a Sci
ence"
)#Char
act
er
vector
temper ature<-c( 98.
1,98.6,101.4)#Numeri
cv ect
or
fl
u_status<-c( FALSE, FALSE, TRUE)#Logi
calVector
num <-c( 1L,2L,3L) #I ntegerVector
color<-c( '
red'
,'
green'
,"yel
l
ow" )
pri
nt(color)
pri
nt(cl
ass( color))

Creat
ingRVectorusi
ngRange(I
nRpr ogrammingthereisaspeci
aloperat
orcall
edRangeor
Colonandt
hiswil
lhel
ptocr
eat
eavect
or.Forexampl
e,i<-1:
10means1,2,3,4,5,
6,7,8,
9,10)

#Creatingasequencefrom 5to13.
v<-5:13
pr
int
( v
)
#Creatingasequencefrom 6.
6to12.6.
v<-6.6:12.6
pr
int
( v
)
#Ifthef i
nalel
ementspeci
fi
eddoesnotbel
ongt
othesequencet
heni
tisdi
scar
ded.
v<-3.8:11.4
pr
int
( v
)

Creati
ngRVectorusingSequence(seq)Oper
ator(I
nthi
sexamplewewillshow y
ou,How t
o
createavect
orusingsequenceoperator
,orsi
mplyseqoper
ator
.TheSequenceoper
atorwi
ll
retur
nval
uessequent
ial
l
y)

#Vectorwi t
hSequence
a<-seq(from =1,t
o=10,by=1)
pr
int
(a)
#Here,from =,t
o=,by=val
uesar
eopti
onsoyoucanr
emov
ethem t
oo
b<-seq(11,to=15,by=1)#Removi
ngfrom
pr
int
(b)
c<-seq(15,25,by=3)#Removi
ngbothfr
om,t
o
pr
int
(c)
d<-seq(
2,20,
2)#r
emov
ingf
rom,
to,
andby
pr
int
(d)

Accessi
ngRVect orEl ements(I nRpr ogrammi ng,Wecanuset heindexposi ti
ontoaccessthe
el
ementsi naVect or.Usingt hisindexv alue,wecanaccessoral t
er/changeeachandev ery
i
ndivi
dualel
ementpr esentinthev ector
.Indexv aluest ar
tsat1andendatnwher enisthevector
l
ength.Forexample,i fwedecl areav ectorthatst ores10el ement stheni ndexst
artsat1and
endsat10.Toaccessoral ter1stv al
ueuseVect or_
Name[ 1]andtoalteroraccessthe10thvalue,
useVector_
Name[10] .Let
sseet heexampl eforbetterunder st
anding:)

#RVectorElement
sAccessing
a<- c("
Indi
a",
"Chi
na"
,"Japan"
,"UK"
,"USA"
,"Russi
a",
"Sr
iLanka"
)
pr
int
(a)
pr
int
(a[
1])
pr
int
(a[
3])
pr
int
(a[
1:3]
)
pr
int
(a[
4:6]
)

AccessusingVect
or(Int
hisexampl
ewewi
l
lshow y
ou,How t
oaccesst
heVect
orel
ement
s
usi
nganotherVect
or.
)

#RVect orEl ementsAccessing


a<- c( "
India","
China"
,"Japan"
,"UK"
,"USA"
,"Russi
a",
"Sr
iLanka"
)
pr
int
(a)
b<-c(2,4,6)
pr
int
(a[b])
pr
int
(a[c(5,7)])
pr
int
(a[c(7,4, 1)
])

Usi
ngNegati
veValues(I
nt hi
sexamplewewi l
lshow y
ou,How toaccesst
heVectorel
ement
s
usi
ngNegat
iveval
uesandtheBooleanval
ues.I
nRVect or
s,Negat
ivei
ndexposi
ti
onareusedt
o
omitt
hosev
alues)

#RVect orElementsAccessing
a<- c("I
ndia",
"China"
,"Japan","
UK"
,"USA"
,"Russi
a",
"Sr
iLanka"
)
pr
int
(a)
b<-c(-
3,-7)
pr
int
(a[
b])
pr
int
(a[
c(-4,-
6,-7)]
)
pr
int
(a[
c(TRUE, FALSE,TRUE, TRUE,
FALSE,TRUE,FALSE)]
)

UseChar acterVect
orsasI ndex(I nthi
sexamplewewi l
lshow you,How toaccesstheVector
elementsusingCharacterVectorsIndexval
ues.From thebelow codesnippety
oucanobser ve
that,wedeclareavectorwithalphabeti
ndexes.Thiscanhelpust oext
ractthevect
orelements
usingthealphabet
s.)

#RVect orElementsAccessing
v<- c("a"=10,"b"=20,"c"=30,"
d"=-
15,
"e"=40)
pr
int
(v)
pr
int
(v[
"a"])
pr
int
(v[
"d"])
pr
int
(v[
c( "
a","
c")
])
Manipul
ati
ngRVect
orEl
ement
s(I
nRPr
ogr
ammi
ng,wecanmani
pul
atet
heVect
orel
ement
sin
f
oll
owingways:
)

#RVect orEl
ementsManipulati
on
a<- c(10,20,30,
-15,40,
-25,60,-
5)
pr
int
(a)
a[
7]<-77
pr
int
(a)
a[
a<0]<-99
pr
int
(a)
#Truncati
ngtheVectorEl
ement s
a<-a[1:
5]
pr
int
(a)
#Deleti
ngVector
a<-NULL
pr
int
(a)

I
mport
antFunct
ions(Fol
l
owingfunct
ionsar
esomeoft
hemostusef
ulf
unct
ionssuppor
tedby
t
heVector
sinRprogr
amming.)

#typeof(Vector):Thismet hodwi l
ltelly
out hedatatypeofthevector
#Sort(Vector):Thismet hodwi l
lhelpust osorttheit
emsinAscendi ngor
der
#length(Vector):Thismet hodwi l
lcountnumberofel ementsinav ect
or
#head( Vector,limit)
:Thi smet hodwi llreturntopsixelements( i
fyouOmitthelimit
).I
fy ou
speci
fythelimi tas4t hen,itwil
lret
ur nfi
rst4elements.
#tail
(Vector,l
imi t
):Thismet hodwi l
lreturnlastsi
xelements(i
fy ouOmitthel
i
mit)
.Ifyouspecif
y
theli
mitas2t hen,itwil
lretur
nlastt woelement s.

#RVect orEl
ementsAccessing
a<- c( 10,20,30,
-15,40,-
25,60,-
5)
print(
a)
#Someoft heImportantFunct
ions
typeof (
a)
l
engt h(a)
head( a)
head( a,4)
tail
(a)
tail
(a,3)
x<-sor t(a)
print(
x)

2.Factor s(Afactori
saspeci alcaseofv
ectorthati
ssolel
yusedt
orepresent
categoricalorordi
nalvari
ables.Inthemedicaldat
asetwearebui
ldi
ng,wemi ghtuseaf
act
ort
o
representgender,becauseitusestwocategori
es:MALEandFEMALE.)

gender<-f actor(c("
MALE" , "
FEMALE","MALE"))
print
(gender )
blood<-f actor(c("
O","
AB" ,"A")
,l
evel
s=c( "
A","
B","
AB"
,"O"
))
print
(blood[1:2])
sympt oms <- f actor(
c("SEVERE" ,"MILD","MODERATE")
,lev
els = c(
"MI
LD"
,"MODERATE"
,
"SEVERE" ),ordered=TRUE)
print
(sympt oms)
colors<-c( '
green',
'
green'
,'
y el
low',
'
red'
,
'r
ed',
'
red'
,
'gr
een'
)
factor_color<-factor(
colors)
print
(factor_color)
pr
int
(nl
evel
s(f
act
or_
col
or)
)

#Fact or s
#Cr eat eav ect orasi nput.
data<-
c("East","
West ","East",
"Nort
h",
"Nor
th"
,"
East
",
"West
",
"West
",
"West
",
"East
",
"Nor
th"
)
pri
nt(dat a)
pri
nt(is.factor(data))
#Appl ythef act orfuncti
on.
factor_dat a<-f actor(dat
a)
pri
nt(fact or_data)
pri
nt(is.factor(factor_data)
)

#Fact orsinDat aFr ame


#Cr eatethev ect or
sf ordataf r
ame.
hei
ght<-c( 132, 151,162,139,166,
147,122)
weight<-c( 48,
49, 66,53,
67,52,40)
gender<-c( "mal e",
"male",
"female",
"f
emale",
"male"
,"
femal
e",
"mal
e")
#Cr eatethedat aframe.
i
nput _data<-dat a.f
rame( height,
weight,
gender
)
pri
nt(i
nput _
dat a)
#Testi fthegendercol umni safactor
.
pri
nt(i
s.factor(
input _data$gender))
#Pr i
ntt hegendercol umnsoseet helevel
s.
pri
nt(i
nput _
dat a$gender )

#Changi ngtheOr derofLevel


s
data<-
c("East",
"West",
"East",
"Nort
h",
"Nort
h","
East"
,"West"
,"West",
"West"
,"
East"
,"
Nor
th"
)
#Cr eatet hefactors
factor_data<-factor(dat
a)
pri
nt(factor_data)
#Appl yt hefactorfuncti
onwithrequiredorderofthelevel
.
new_ order_data<-factor(
fact
or_data,
level
s=c( "
East",
"West",
"Nort
h")
)
pri
nt(new_ order_data)

#Generati
ngFactorLevel
s
v<-gl
(3,4,l
abel
s=c( "
Tampa"
,"Seat
tl
e",
"Bost
on"
))
pr
int
(v)

3.Lists(Al i
sti
sadat ast
ructur
e,muchl i
keav ector,inthatiti
susedf orstor
inganor deredset
ofelements.However,whereav ect
orrequir
esallitselementst obethesamet ype,ali
stall
ows
dif
ferenttypesofelement
st obecol l
ected.Duet ot hi
sf l
exibi
l
ity
,li
stsareof tenusedt ostore
vari
oust y
pesofinputandoutputdataandset sofconf i
gurati
onparamet er
sformachi nelear
ning
model s)

#CreateRLi st
l
ist
.data<-l
i
st (
"R"
,"Basi
cPr
ogr
ammi
ng"
,TRUE,
c(10,
20,
30)
,95,
105.
61)
pri
nt(
li
st.
data)

Cr
eat
ingRLi
stusi
ngVect
ors

Example-1
>subject
_name<-c(
"Mat
hemat
ics"
,"St
ati
sti
c","
Machi
neLear
ning"
,"
Dat
aSci
ence"
)#Char
act
er
vect
or
>temper atur e<-c( 98.1,98.6,101.4)#Numer i
cv ect
or
>flu_stat
us<-c( FALSE, FALSE, TRUE)#Logi calVector
gender<-f act or( c("
MALE" ,"
FEMALE" ,"
MALE" ))
>num <-c( 1L, 2L,3L) #I ntegerVector
>blood<-f act or(c("O","AB","
A"),l
evel
s=c( "
A" ,"B","
AB","
O"))
> sympt oms <-f actor(c("
SEVERE" ,"MILD" ,"MODERATE" ),levels = c(
"MI
LD","MODERATE"
,
"SEVERE") ,or der ed=TRUE)
l
ist.
data<-l ist( f
ul l
name=subj ect_name[1],
temper atur
e=t emperatur
e[1]
,
fl
u_status=f lu_ status[1],
gender=gender [
1],blood=blood[1]
,sy
mpt oms=sy mptoms[
1])
pri
nt(subject 1)

Exampl e-2
#Cr eati
ngt hr
eev ect
ors
vect.
a<-c( 10.25,20.45,30.75,40.
85)
vect.
b<-c( 25,50, 75,100,125)
vect.
c<-c( "I
ndia","Chi
na","Japan"
,"Russi
a",
"USA"
)
#Cr eati
ngl i
st
l
ist.
data<-l i
st(vect.
a,vect
.b,vect.
c)
pri
nt(l
ist.
data)

Example-3
vect.
a<-c(10.25,30.75,20.
45,40.85)
vect.
b<-c("I
ndia","
Japan","Russi
a","
Chi
na"
,"USA"
)
l
ist.
data<-li
st(v
ect.a,vect
.b)
pri
nt(l
i
st.
data)

#Creati
ngRLi stusi
ngMat ri
x
A<-mat ri
x(c(1:12),
nrow=3)
vect.
c<-c(50, 75,25,100,125)
l
ist.
mixed<-list(A,l
ist
.dat
a,vect
.c)
pri
nt(l
i
st.
mi xed)

Creati
ngNamedLi sti
nR
l
ist.
data<-list
("Company
"="
RBasi
cPr
ogr
ammi
ng"
,"Fl
ag"=TRUE,"
prod"=c(
10,20,30)
,"v
al"=
95,"sale"=105.61)
pri
nt(l
ist.
data)

Creati
ngNamedLi stinRusingnamesf uncti
on
vect.
x<-c(10, 30,50, 70)
vect.
y<-c("India","
Japan","
UK","Russi
a","
China"
,"USA"
)
l
ist.
a<-list
(vect .
x,vect.
y)
#AssigningNames
names( l
i
st.a)<-c( "
Num_ Vector
","Count
ry"
)
pri
nt(l
i
st.a)

matrix.A<-mat r
ix(c(
1:12),
3,4)
vect.
z<-c( 55, 75,25,105,125)
l
ist.
mi xed<-list(matri
x.A,l
ist
.a,
vect.
z,"
RBasicProgramming")
names( li
st.
mi xed)<-c( "
Num_ Matr
ix"
,"I
nner
_Li
st"
,"Rand_
vector
","
Company
")
pri
nt(li
st.
mi xed)

Accessi
ngRLi stElements
vect
.a<-c(
10.25,30.75,20.45,
40.85)
vect
.b<-c(
"I
ndi a"
,"Japan"
,"Russi
a","
Chi
na"
,"USA"
)
vect
.c<-c(
50,75, 25,100,125)
A<-mat
ri
x(c(
1:12)
,3,
4)

l
ist
.data<-l
i
st(A,
vect
.a,
"RBasi
cPr
ogr
ammi
ng"
,vect
.b,
95,
vect
.c)
pri
nt(
li
st.
data)

#AccessingFir
stEl
ement
pr
int
(l
ist
.data[
1])

#AccessingFourthEl
ement
pr
int
(l
ist
.data[
4])

Accessi
ngRListit
emsusi ngNames
vect
.x<-c(
10,30,50,70)
vect
.y<-c(
"I
ndia"
,"Russi
a","
Japan"
,"UK"
,"Chi
na"
,"USA"
)

l
ist
.dat
a<-list
(vect.
x,vect
.y)
names(l
ist
.data)<-c("
Numeric_
Vect
or"
,"Count
ry"
)

matri
x.A<-matri
x(c(
1:12)
,3,4)
vect
.z<-c(55,
75,25,105,125)

l
ist
.mixed<-li
st(
matrix
.A,l
i
st.
dat
a,vect.
z,"RBasicProgr
ammi ng"
)
names(li
st.
mixed)<-c("
Numeri
c_Matri
x",
"Nested_Li
st"
,"Random_vect
or"
,"Company
")
pri
nt(
li
st.
mixed)

#AccessingVector.
zEl
ement
s
pr
int
(l
ist
.mixed$Random_v
ect
or)

#AccessingVector
.zEl
ement
s
pr
int
(l
ist
.mixed$Numeri
c_Mat
ri
x)

#AccessingNestedLi
stEl
ement
s
pr
int
(l
ist
.mixed$Nest
ed_Li
st)

Accessi
ngRLi stit
emsusi ngBooleanvector
vect
.a<-c(
10.25,30.75,20.45,
40.85)
vect
.b<-c(
"I
ndi a"
,"Japan"
,"Russi
a","
China",
"USA"
)
vect
.c<-c(
50,75, 25,100,125)

A<-mat
ri
x(c(
1:12)
,3,
4)

l
ist
.data<-l
i
st(A,
vect
.a,
"R Pr
ogr
ammi
ng"
,vect
.b,
95,
vect
.c)
pri
nt(
li
st.
data)

#AccessingListEl
ementusi
ngBooleanVector
pr
int
(l
ist
.data[
c(FALSE,
FALSE,TRUE,FALSE,
TRUE,
TRUE)
])

#AccessingAllEl
ementexcept1
pr
int
(l
ist
.data[
-1]
)

#AccessingAllEl
ementexcept4
pr
int
(l
ist
.data[
-4]
)

#Accessi
ngAl
lEl
ementexcept1and6t
hel
ement
pr
int
(l
ist
.dat
a[c(
-1,
-6)
])

Manipul
ati
ngRListElements
vect
.x<-c(
10,30,50,70)
vect
.y<-c(
"I
ndia"
,"Russi
a",
"Japan"
,"UK"
,"Chi
na"
,"USA"
)

l
ist
.dat
a<-list
(vect.
x,vect
.y)
names(l
ist
.data)<-c("
Numeric_
Vect
or"
,"Count
ry"
)

matri
x.A<-matri
x(c(
1:12)
,3,4)
vect
.z<-c(55,
75,25,105,125)

l
ist
.mixed<-li
st(
matrix
.A,l
i
st.
dat
a,95,vect.
z,"
R Pr ogramming"
)
names(li
st.
mixed)<-c("
Numeri
c_Matr
ix",
"Nested_
List"
,"f
avNum","
Random_
vect
or"
,"Company
")
pri
nt(
li
st.
mixed)

l
ist
.mixed$Company<-"
Tutor
ti
alPr
ogr
ammi
ng.
org"
pri
nt(
li
st.
mixed$Company)

l
ist
.mixed$Random_vect
or<-c(22,
44,
66,
88)
pri
nt(
li
st.
mixed$Random_vect
or)

l
ist
.mixed$Numeri
c_Mat
ri
x<-NULL
pri
nt(
li
st.
mixed)

MergingtwolistsinRprogramming
vect
.a<-c(10.25,30.75,
20.45,
40.85)
vect
.b<-c("I
ndia","
Japan",
"Russi
a","
Chi
na"
,"USA"
)

#Creati
ngList1from t
hoset
woVect
orsVect
.a,
Vect
.b
l
ist
.x<-li
st(
vect
.a,
95,vect
.b)
pri
nt(
li
st.
x)

#Decl
aredOne4*3mat ri
x
A<-matri
x(c(
1:12)
,4,
3)

#Creati
ngsecondl
istwi
thoneSt
ri
ng,Matr
ix,
andav
ect
or
l
ist
.y<-l
ist
("
R Programming"
,A,
c(5,
10,15))
pri
nt(
li
st.
y)

#Combi ni
ngorMer gi
ngt
woLi
st
l
ist
.z<-c(l
ist
.x,
li
st.
y)
pri
nt(
li
st.
z)

Conv ertListt oVectori


nR
l
ist.x<-li
st( 1:
15)
l
ist.y<-li
st( 25:40)
print(
li
st.x)
print(
li
st.y)
typeof(l
ist.x)

vect
.a<-unl
ist
(l
ist
.x)
pri
nt(
vect
.a)

v
ect
.b<-unl
i
st(
li
st.
y)
pr
int
(vect
.b)

4.Matrices(Rpr ov
idesotherstruct
uresthatstorev al
uesinatabular
form.Amat r
ixi
sadat astructur
ethatrepresentsat wo-di
mensionalt
abl
ewith
rowsandcol umnsofdat a.Likevect
ors,Rmat r
ixescancontai
nanyonet ypeof
data,al
t houghtheyaremostof tenusedf ormat hemati
caloperati
onsand,ther
efor
e,t
ypi
cal
l
y
storeonlynumericdata.)

>m1<-
mat
ri
x(1:
30,
nrow=5,
ncol
=6)#Fil
l
edcolumnwisebydef
ault
>m2<-
mat
ri
x(1:
30,
nrow=5,
ncol
=6,
byrow=TRUE) #Fil
ledr
owwise

>x=mat r
ix(c(
'a'
,
'a'
,
'b'
,
'c'
,
'b'
,
'a'
),
nrow=2,
ncol
=3,
byr
ow=TRUE)
pr
int
(x)

Cr
eateRMat rixusi
ngcbi
ndandr
bind
A<-c(1,2,3)
B<-c(20,30,40)
X<-cbind(A,
B)
pr
int
(X)
Y<-rbi
nd(A,B)
pr
int
(Y)

Defi
ningRownamesandCol
umnnamesformat
ri
xinR
A<-mat r
ix(
20:
31,
3,4,
byr
ow=TRUE,di
mnames=list
(c(
"X"
,"Y"
,"Z"
),c(
"A"
,"B"
,"C"
,"D"
)))
pr
int
(A)

#Def i
ningRownamesandCol umnnamesofMat r
ixi
nR
row.names<-c( "
Row1" ,"
Row2" ,"
Row3")
column.names<- c(
"Col1"
,"Col2",
"Col
3",
"Col4"
)
B<-mat ri
x(c(
1:12),nr
ow=3, dimnames=list(
row.
names,
col
umn.
names)
)
pri
nt(B)

ImportantFuncti
onforMat
ri
xinR
A<-mat ri
x(c(
1:12),
nrow=3,ncol
=4,
byr
ow=TRUE)
print
(A)
class(
A)
dim(A)

RVectorwithRecy
cli
ngelement
s(Al lt
heabov
especi
fyexampl
esareworki
ngfi
nebecausewe
speci
fyexactel
ementsinRowsandcol umnssuchas12elementsar
rangedin3rowsand4
col
umns.Int hi
sexamplewewi llshow y
ou,Whatwil
lhappenifwespecif
ylessnumberof
el
ement)

A<-matri
x(c(
44:
46)
,nr
ow=3,
ncol
=3)
pr
int
(A)

B<-mat r
ix(c(
44:46),
nrow=3, ncol=3,
by r
ow=TRUE)
pr
int(
B)
Accessi
ngRMat ri
xElements
A<-mat r
ix(c(
1:12)
,nrow=3, ncol
=4,byrow=TRUE)
pr
int(
A)
#Accesst heelementat1strowand2ndcol umn.
pr
int(
A[1,2])
#Accesst heelementat3rdrowand4thcolumn.
pr
int(
A[3,4])
#Accessonl yt he2ndr ow.
pr
int
(A[2,])
#Accessonl yt he4thcolumn.
pr
int
(A[,4])
#AccessCompl eteMatri
x.
pr
int
(A[, ])
#Accesst heel ementsat1st,3rdrowand2nd,4t
hcol
umn.
pr
int
(A[c(1, 2)
, c(3,4)]
)
#AccessAl lt
heel ementat2ndand3r dr
ow.
pr
int
(A[c(2, 3)
, ])
#AccessAl lt
heel ementat1stand4thColumn.
pr
int
(A[, c(1,4)])
#AccessAl lt
heel ementexcept2ndrow.
pr
int
(A[-2,])
#AccessAl lt
heel ementexcept2ndrowand3rdCol
umm.
pr
int
(A[-2,-3])
#AccessAl lt
heel ementexcept3rdand4thCol
umm.
pr
int
(A[,c(-3,-4) ]
)

Modif
yingRMat ri
xElement
s
A<-mat r
ix(
c(1:
9),nr
ow=3, ncol
=3)
pr
int
(A)
A[
2,2]<-100
pr
int
(A)
A[
A<5]<-222
pr
int
(A)

RMat
ri
xAddi
ti
on,
Subt
ract
ion,
Mul
ti
pli
cat
ionandDi
vi
si
on

#Creat
e2x3mat r
ices.
A<- matr
ix(c(
15, 34,
38,
44,
75,
93)
,nr
ow=2)
B<- matr
ix(c(
10, 20,
30,
40,
50,
60)
,nr
ow=2)
pr
int
(A)
pr
int
(B)

#Addii
ngt
woMat
ri
ces
pr
int
(A+B)

#Subtr
act
ionOneMat
ri
xfr
om anot
her
pr
int
(A-B)

#RMatri
xMul
ti
pli
cat
ion
pr
int
(a*b)

#Matri
xDi
visi
on
pr
int
(a/b)

5.Array
s(Whi l
emat r
icesareconf
inedtotwodimensions,ar
rayscanbeofanynumberof
dimensi
ons.Thearr
ayf unct
iont
akesadi m at
tri
but
ewhi chcreat
estherequi
rednumberof
dimensi
on.I
nthebel
owexampl ewecr eat
eanarr
aywithtwoelementswhi
chare4x4matri
ces
each)

#CreateArr
ayinR
a<-arr
ay(c(
'gr
een'
,
'yel
l
ow'
),
dim=c(
4,4,
3))
pr
int
(a)
z<-
arr
ay(1:
30,
c(2,
3,
5))
pri
nt(
z)

#Cr eatetwov ectorsofdi f


ferentl
engths.
vector1<-c( 5,
9,3)
vector2<-c( 10,11,12,13,
14,15)
#Taket hesev ectorsasi nputtothearray.
result<-array(c(vector1,
vector2),
dim=c(3,
3,
2))
print
(result
)

#Def iningRownamesandCol umnnamesf orArrayi


nR
#Cr eatetwov ectorsofdi ff
erentlengt
hs.
vector1<-c( 5,
9,3)
vector2<-c( 10,11,12,13,14,15)
column. names<-c( "COL1" ,
"COL2","
COL3")
row.names<-c( "
ROW1" ,"
ROW2" ,
"ROW3" )
mat ri
x.names<-c( "Mat ri
x 1"
,"Matr
ix2")
#Taket hesev ectorsasi nputtothear r
ay.
result<-array(c(vector1,vector2),
dim=c(3,
3,
2),di
mnames=
l
ist(column.names, r
ow. names, mat r
ix.
names))
print(
result
)

#Accessi ngRAr rayEl ement s


#Cr eatetwov ect orsofdi ff
erentlengt
hs.
vector1<-c( 5,9,3)
vector2<-c( 10,11, 12,13,14,15)
column. names<-c( "COL1" ,
"COL2","
COL3")
row.names<-c( "ROW1" ,"
ROW2" ,
"ROW3" )
mat ri
x.names<-c( "Mat ri
x 1"
,"Matr
ix2")
#Taket hesev ect orsasi nputtothear r
ay.
result<-array(c(vect or1,vector2),
dim=c(3,3,
2),
dimnames=
l
ist(column.names, r
ow. names, mat r
ix.
names) )
#Pr intthethirdr owoft hesecondmat ri
xofthearray
.
print(r
esult
[3,,
2])
#Pr inttheelementi nt he1str owand3r dcolumnoft he1stmat
ri
x.
print(r
esult
[1,3,1])
#Pr intthe2ndMat r
ix.
print(r
esult
[,,
2])

#Mani pul
ati
ngAr rayEl ement s
#Cr eatetwov ectorsofdi ff
erentl
engths.
vector1<-c(5,9,3)
vector2<-c(10, 11,12,13,14,15)
#Taket hesev ectorsasi nputtothearray.
array1<-array(c(vect or1,vector
2),
dim=c(3,
3,2)
)
#Cr eatetwov ectorsofdi ff
erentl
engths.
vector3<-c(9,1,0)
vector4<-c(6,0,11,3,14,1,2,6,
9)
array2<-array(c(vect or1,vector
2),
dim=c(3,
3,2)
)
#cr eatemat r
icesf r
om t hesear r
ays.
mat ri
x1<-ar r
ay 1[,
,2]
mat ri
x2<-ar r
ay 2[,
,2]
#Addt hemat rices.
result<-mat r
ix1+mat ri
x 2
print(
result
)
#Cal culationsAcr ossAr rayElements
#Cr eatetwov ectorsofdi ffer
entlengths.
vector1<-c( 5,9,
3)
vector2<-c( 10,11,12,
13,14,15)
#Taket hesev ectorsasi nputt othearray.
new.ar r
ay<-ar ray(c(vector1,v
ector2)
,dim=c(3,
3,
2))
print
(new. array)
#Useappl ytocalculatethesum oft her owsacrossal
lthemat
ri
ces.
result<-appl y(
new. array
, c(1)
,sum)
print
(result)

#RAr r
ayAddit i
onandSubt ract
ion
vect
1<-c(10, 20,40)
vect
2<-c(55, 67,89,96,100)
A<-array
(c(vect1,vect
2),dim =c(3,
4,2)
)
pri
nt(
A)
mat.A<-A[,
,1]
mat.B<-A[,
,2]
pri
nt(
mat.A+mat .B)
pri
nt(
mat.B-mat .
A)

6.DataFrames(Byf ar,themostimpor tantRdatastructureuti


li
zedinmachinelearningisthe
dat
af rame,astruct
ureanalogoustoaspr eadsheetordat abase,si
nceithasbot hr owsand
col
umnsofdat a.I
nRt erms,adataframecanbeunder stoodasal i
stofvector
sorfact or
s,each
havi
ngexactl
ythesamenumberofv alues.Becausethedat afr
amei sl
it
eral
lyali
stofv ect
ortype
obj
ects,i
tcombinesaspectsofbot
hv ectorsandli
sts.)

Letscreat
eadataf r
amef orourpati
entdataset
.Usingthepat
ientdat
avect
orswecr
eat
ed
previ
ousl
y,t
hedat
a.f
rame(
)funct
ioncombinesthem i
ntoadat
afr
ame:

>subj ect_
name<-c( "Mathemat i
cs","St
ati
sti
c","MachineLear
ning",
"Dat
aScience"
)#Char
act
er
vector
>temper atur
e<-c( 98.
1,98.6,101.4)#Numer i
cv ect
or
>flu_stat
us<-c( FALSE,FALSE, TRUE)#LogicalVector
gender<-factor(c("
MALE" ,
"FEMALE" ,"
MALE" ))
>num <-c( 1L,2L,
3L) #I ntegerVector
>blood<-f actor(
c("O"
,"AB","
A"),l
evel
s=c( "
A","B","
AB"
,"O")
)
> sy mptoms <-f act
or(c(
"SEVERE" ,"MILD","MODERATE" ),l
evels = c(
"MI
LD","MODERATE",
"SEVERE"),ordered=TRUE)

>pt_dat
a<-data.
fr
ame(subj
ect
_name, t
emper
atur
e,f
lu_
stat
us,
gender,
blood,
symptoms,st
ri
ngsAsFactor
s=FALSE)

#RCr eateDataFrame
I
d<-c(1:10)
Name<-c( "
John","Rob","Ruben","Christ
y",
"Johnson","
Mill
er"
,"Carl
son","
Ruiz"
,"Yang",
"Zhu"
)
Occupati
on<-c("Professional
","Programmer ",
"Management "
,"Cler
ical
",
"
Dev eloper"
,"Programmer "
,"Management "
,"Cl
eri
cal"
,"Devel
oper",
"Pr
ogrammer
")
Sal
ary<-c(80000,70000, 90000, 50000,60000, 75000,92000,68000,55000,82000)

employ
ee<-dat
a.f
rame(
Id,
Name,
Occupat
ion,
Sal
ary
)
pr
int
(empl
oyee)
#Cr eateNamedDat aFramei nRPr ogrammi ng
Id<-c(1:6)
Name<-c( "
John" ,
"Rob","
Chr i
sty"
,"
Johnson","Mill
er"
,"Zhu")
Occupat i
on<-c( "Professi
onal","
Management "
,"Developer"
,"Pr
ogrammer",
"Cl
eri
cal
","Admin"
)
Salary<-c(80000, 90000, 75000,92000,68000, 82000)
#Wear eassi gningnewnamest otheColumns
empl oyee<-dat a.fr
ame( "
Empi d"=Id,"
Full_
Name"=Name, "
Professi
on"=Occupat
ion,"i
ncome"=
Salary)
pri
nt(employee)
#Namesf unct i
onwi lldi
splaytheIndexNamesofeachI tem
pri
nt(names( empl oyee))

#AccessingRDat aFr ameEl ement s


Id<-c(1:
6)
Name<-c( "John" ,
"Rob","
Chr i
sty",
"Johnson","
Mill
er"
,"Zhu"
)
Occupati
on<-c( "Professi
onal "
,"Management ","
Developer
","Pr
ogrammer "
,"Cl
eri
cal
","
Admi
n")
Salar
y<-c( 80000,90000, 75000,92000, 68000,82000)
employee<-dat a.fr
ame( I
d,Name, Occupati
on,Salar
y)
pri
nt(
empl oyee)
#Accessingal ltheElement s(Rows)Pr esentintheNameI tems(Column)
employee["Name" ]
#Accessingal ltheElement s(Rows)Pr esentinthe3rdColumn( i
.e.
,Occupati
on)
employee[3]#IndexVal ues: 1=Id, 2=Name, 3=Occupat ion,4=Salary

#Accessi
ngRDataFrameEl
ement
s
empl
oyee[
["Name"
]]
empl
oyee[
[3]]

#Accessi
ngallt
heEl
ement
s(Rows)Pr
esenti
ntheNameItem (
Col
umn)
empl
oyee$Name
#Accessi
ngallt
heEl
ement
s(Rows)Pr
esenti
ntheSal
aryI
tem (
Col
umn)
empl
oyee$Sal
ary

#Accessi
ngEl
ementat1stRowand2ndCol
umn
empl
oyee[
1,2]

#Accessi
ngEl
ementat4t
hRowand3r
dCol
umn
empl
oyee[
4,3]

#Accessi
ngAll
Element
sat5t
hRow
empl
oyee[
5,]

#Accessi
ngAll
Item oft
he4t
hCol
umn
empl
oyee[
,4]

#Accessi
ngIt
em at1st
,2ndRowsand3r
d,4t
hCol
umns
empl
oyee[
c(1,
2),
c(3,4)
]

#Accessi
ngIt
em at2nd,
3rd,
4thRowsand2nd,
4thCol
umns
empl
oyee[
2:4,
c(2,
4)]

#Accessi
ngAllI
tem at2nd,
3rd,
4th,
5thRows
empl
oyee[
2:5,
]
#Accessi
ngAllI
tem of2ndand4t
hCol
umn
empl
oyee[
c(2,
4)]

#Accessi
ngIt
em at2nd,4t
hRowsofNameCol
umns
empl
oyee$Name[
c(2,4)]

#Accessi
ngIt
em at2nd,3rd,
4th,
5thRowsofOccupat
ionCol
umn
empl
oyee$Occupat
ion[
2:5]

#Modifyi
ngIt
em at2ndRowand3r
dCol
umn
employ
ee[2,
3]<-100000
pr
int
(employ
ee)

# Modi f
yingAllI
tem of1stCol
umn
employ
ee[,1]<-c(
10:15)
pr
int
(employee)

#AddingExt
raRow
r
bind(
employee,
li
st(
7,"
Progr
ammi
ng"
,105505)
)

#AddingExtr
aColumn
Occupat
ion<-c(
"Management",
"Dev
eloper
","
User
","
Progr
ammer
","
Cler
ical
","
Admi
n")
cbi
nd(employ
ee,Occupat
ion)

Youmi
ghtnoticesomethingnewi ntheprecedi
ngcode.Weincl
udedanaddi
tional
par
amet
er:str
ingsAsFactors=FALSE.Ifwedonotspecifyt
hisopt
ion,
Rwill
aut
omat
ical
lyconverteverychar
actervectort
oafactor
.

Thisfeat
urei soccasi
onall
yuseful
,butal
sosomet imesunwar r
anted.Her
e,f
or
example,thesubject_
namef i
eldisdefi
nit
elynotcategor
icaldat
a,asnamesarenotcategor
iesof
val
ues.Ther efor
e,sett
ingthestri
ngsAsFactor
sopt i
ontoFALSEal l
owsust oconv
ertcharact
er
vector
stof actor
sonlywhereitmakessensef ortheproj
ect.

Whenwedispl
ayt
hept_
datadatafr
ame,
weseet
hatt
hest
ruct
urei
squi
tedi
ff
erentf
rom t
hedat
a
st
ruct
ureswewor
kedwit
hprevi
ousl
y:

>pr
int
(pt
_dat
a)

#=============================================================================
====
Operator
sinR
#=============================================================================
====

#--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
----
--
---
--
--
--
--
--
---
--
--
----
--
--
--
--
--
--
--
--
--
-
# Arit
hmetic/Relati
onalOperat
ors
#--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
----
--
---
--
--
--
--
--
---
--
--
----
--
--
--
--
--
--
--
--
--
-
Operat
orsarepar t
icularsy mbolswhi choper ateonsomev al
uesandproduceanout put
.The
val
uesareknownasOper ands.
9<-4+5
Here4and5ar eOper andsand( +),(<-)signsaretheoperat
ors.Theypr
oducetheout
put9.

Ar
it
hmet
icOper
ator
s
1.ADDSTWOVECTORS
a<-c(2,
5.5,
6)
b<-c(8,
3,4)
pri
nt(
a+b)

2.SUBTRACTSSECONDVECTORFROM THEFI
RST
v<-c(2,5.
5,6)
t<-c(
8,3,4)
pri
nt(
v-t
)

3.MULTIPLI
ESBOTHVECTORS
v<-c(2,
5.5,
6)
t<-c(
8,3,4)
pri
nt(
v*t
)

4.DIVIDETHEFIRSTVECTORWI
THTHESECOND
v<-c(2,5.5,
6)
t<-c(8,3,4)
pri
nt(v/t
)

5.GIVETHEREMAI
NDEROFTHEFI
RSTVECTORWI
THTHESECOND
v<-c(2,5.
5,6)
t<-c(8,3,
4)
pri
nt(v%%t)

6.THERESULTOFDI
VISI
ONOFFI
RSTVECTORWI
THSECOND(
QUOTI
ENT)
v<-c(2,
5.5,
6)
t<-c(
8,3,4)
pri
nt(
v%/%t)

7.THEFIRSTVECTORRAI
SEDTOTHEEXPONENTOFSECONDVECTOR
v<-c(2,
5.5,
6)
t<-c(
8,3,4)
pri
nt(
v^t
)

Rel
ati
onal
Oper
ator
s

1.CHECKSI FEACHELEMENTOFTHEFI
RSTVECTORI
SGREATERTHANTHECORRESPONDI
NG
ELEMENTOFTHESECONDVECTOR.
v<-c(2,
5.5,
6,9)
t<-c(
8,2.
5,14,9)
pri
nt(
v>t)

2.CHECKSI FEACH ELEMENTOFTHEFI


RSTVECTORI
SLESSTHAN THECORRESPONDI
NG
ELEMENTOFTHESECONDVECTOR.
v<-c(2,
5.5,
6,9)
t<-c(
8,2.
5,14,9)
pri
nt(
v<t)

3.CHECKSI FEACH ELEMENTOFTHEFI


RSTVECTORI
SEQUALTO THECORRESPONDI
NG
ELEMENTOFTHESECONDVECTOR.
v<-c(2,
5.5,
6,9)
t<-c(
8,2.
5,14,9)
pri
nt(
v==t)
4.CHECKS I FEACH ELEMENT OFTHEFIRST VECTOR I
S LESS THAN OR EQUALTO THE
CORRESPONDI NGELEMENTOFTHESECONDVECTOR.
v<-c(2,
5.5,
6,9)
t<-c(
8,2.
5,14,9)
pri
nt(
v<=t)

5.CHECKSI FEACHELEMENTOFTHEFI RSTVECTORISGREATERTHANOREQUALTOTHE


CORRESPONDI NGELEMENTOFTHESECONDVECTOR.
v<-c(2,
5.5,
6,9)
t<-c(
8,2.
5,14,9)
pri
nt(
v>=t)

6.CHECKSI FEACHELEMENTOFTHEFI
RSTVECTORI
SUNEQUALTOTHECORRESPONDI
NG
ELEMENTOFTHESECONDVECTOR.
v<-c(2,5.
5,6,
9)
t<-c(
8,2.5,
14,9)
pri
nt(
v!=t)

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
# Logical
/Assi
gnment/Miscel
laneousOper
ator
s
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--

1.ITISCALLEDELEMENT- WISELOGI
CALANDOPERATOR.ITCOMBI
NESEACHELEMENTOF
THEFI RSTVECTORWI TH THECORRESPONDI
NG ELEMENTOFTHESECOND VECTORAND
GIVESAOUTPUTTRUEI FBOTHTHEELEMENTSARETRUE.
a<-c(3,
1,TRUE,2+3i)
b<-c(4,
1,FALSE,2+3i
)
pri
nt(
a&b)

2.ITISCALLEDELEMENT- WISELOGI
CALOROPERATOR.I
TCOMBI
NESEACHELEMENTOFTHE
FIRSTVECTORWI THTHECORRESPONDI
NGELEMENTOFTHESECONDVECTORANDGIVESA
OUTPUTTRUEI FONETHEELEMENTSI STRUE.
x<-c( 3,
0,TRUE,2+2i)
y<-c( 4,
0,FALSE,2+3i
)
print
(x|y
)

3.ITISCALLED LOGICALNOT OPERATOR.TAKESEACH ELEMENTOFTHEVECTORAND


GIVESTHEOPPOSI TELOGI
CALVALUE
p<-c(3,
0,TRUE,
2+2i
)
pr
int(
!p)

#-
---
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
4.CALLEDLOGI CALANDOPERATOR.TAKESFI RSTELEMENTOFBOTH THEVECTORSAND
GIVESTHETRUEONLYI FBOTHARETRUE.
a<-c(3,
0,TRUE,2+2i
)
b<-c(1,
3,TRUE,2+3i
)
pr
int(
a&&b)

5.CALLED LOGI CALOR OPERATOR.TAKESFI


RSTELEMENTOFBOTH THEVECTORSAND
GIVESTHETRUEONLYI FBOTHARETRUE.
x<-c(0,
0,TRUE,
2+2i)
y<-c(0,
3,TRUE,
2+3i)
pr
int(
x||
y)
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
# Assi
gnmentOper at
ors
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

6.CALLEDLEFTASSI
GNMENT
x1<-c(3,
1,
TRUE,
2+3i
)
pri
nt(
x1)

x2<<-c(
3,1,
TRUE,
2+3i
)
pr
int
(x2)

x3=c(3,
1,TRUE,
2+3i
)
pr
int
(x3)

7.CALLEDRI GHTASSIGNMENT
c(3,1,
TRUE,
2+3i)-
>y1
print(
y1)

c(
3,1,
TRUE,
2+3i
)->>y
2
pr
int(
y2)

#-
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
# Miscel
laneousOperat
ors
#-
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
8.
COLONOPERATOR.I TCREATESTHESERI ESOFNUMBERSINSEQUENCEFORAVECTOR.
a<-2:8
pr
int(
a)

9.THISOPERATORI
SUSEDTOI
DENTI
FYI
FANELEMENTBELONGSTOAVECTOR.
x1<-8
x2<-12
f<-1:
10
pri
nt(
x1%in%f)
pri
nt(
x2%in%f)

10.THISOPERATORI SUSEDTOMULTI PLYAMATRI


XWI
THI
TSTRANSPOSE.
m =mat r
ix(c(
2,6,
5,
1,
10,
4),
nrow=2,
ncol
=3,by
row=
TRUE)
r=m %* %t(m)
pri
nt(
r)

#=============================================================================
====
Programmingwi t
hR
#=============================================================================
====

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
-
# if,
ifel
se,i
felseif&switchStat
ement
s
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
-

#--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
---
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
TheR ifst
atement  
i
susedt otestthecondit
ion.I
tchecksbool
eancondi
ti
on:
tr
ue 
or 
fal
se.Ther
e
arevari
oustypesofifstatementinR.
i
fstatement
i
f-el
sestatement
nestedifstat
ement
i
f-el
se-i
fladder

1.
if(TheRI fStatementisthebasicdeci
sion-
makingstatementi
nr ealprogrammingwor ld.It
al
lowsthecompi l
ertotestthecondi
ti
onfir
st,anddependi
ngupont heresultitwi
llexecutethe
st
atements.I
fthetestcondi
ti
onistr
uethenonlyst
atementswithi
ntheifstatementisexecuted.)

Her
eisthesyntaxforani
fst
atementi
nR:
if
(condi
ti
on){
//
codetobeexecut
ed
}

#EXAMPLE- 1
number<-as.
int
eger
(readl
i
ne(
prompt
="Pl
easeEnt
eranyi
ntegerVal
ue:
"))

i
f(number>1){
pr
int
("
Youhav
eent
eredPOSI
TIVENumber
")
}

#EXAMPLE-2
hot<-FALSE
temp<-60
i
f(temp>80) {
hot<-TRUE
}
pri
nt(
hot)

#EXAMPLE-3
temp<-100
i
f(temp>80) {
hot<-TRUE
}
pri
nt(
hot)

#EXAMPLE-4
i
f(1==1)
{pr
int
('
hi
')
}

#EXAMPLE-5
x<-30L
i
f(
is.
integer(
x))
{
pri
nt(
"XisanI
nteger
")
}

2.
if-
else(TheRi f
-el
sestatementalsot
est
sthecondi
ti
on.I
texecut
est
hei
fbl
ocki
fcondi
ti
oni
s
tr
ueot her
wiseel
seblockisexecut
ed.)

I
thasthesyntax:
i
f(condi
ti
on){
//
codeifcondi
ti
oni
strue
}el
se{
//
codeifcondi
ti
oni
sfalse
}

#EXAMPLE-1
my.
age<-as. int
eger(
readl
ine(prompt="Pl
easeEnteryourAge:"
))
i
f(my.age<18){
print
("
Youar eNotaMaj or.
")
print
("
Youar eNotEligibletoWork")
}el
se{
i
f( my.age>=18&&my .age<=60) {
print(
"YouareEli
gibletoWork")
print(
"Pl
easefil
ltheApplicat
ionForm andEmailtous")
}else{
print(
"AspertheGov ernmentRules,Youar
et ooOldtoWor
k")
print(
"Pl
easeCollectyourpension!
")
}
}

#EXAMPLE-2
temp<-30
i
f(temp>90) {
pr
int
("
Hotout
side!
")
}else{
pr
int
("
It
snott
oohott oday
!")
}

#EXAMPLE-3
x<-c("what",
"i
s",
"t
ruth"
)
i
f("
Truth"%in%x) {
pri
nt(
"Truthisfound")
}el
se{
pri
nt(
"Truthisnotfound"
)
}

3.i
f-
else-i
f.
.( Thei f-
else-
ifl
adderstat
ementexecut
esonecondi
ti
onf
rom mul
ti
plest
atement
s.)
I
thast hesy ntax:
if
(condi ti
on1){
/
/codet obeexecut edifcondi
ti
on1istr
ue
}elseif(condit
ion2){
/
/codet obeexecut edifcondi
ti
on2istr
ue
}
elseif(condit
ion3){
/
/codet obeexecut edifcondi
ti
on3istr
ue
}
..
.
else{
/
/codet obeexecut edifal
lthecondi
ti
onsaref
alse
}

#EXAMPLE-1
my.
mar ks<-as.
integer(r
eadl
ine(
prompt ="PleaseEnt
ery
ourTot
alMar
ks:
"))
i
f(my.marks>=550){
print
("
Congratulati
ons!
!"
)
print
("
Youareel igi
blef
orFullScholarship"
)
}el
seif(my .
mar
ks>=490) {
print
("
Congratulati
ons!
!"
)
pri
nt(
"Youareel
igi
blefor50%Scholar
ship"
)
}el
seif(
my .
marks>=400) {
pri
nt(
"Congr
atul
ati
ons!!
")
pri
nt(
"Youareel
igi
blefor10%Scholar
ship"
)
}el
se {
pri
nt(
"YouareNOTel i
gibl
eforSchol
arshi
p")
pri
nt(
"Wearereall
ySorryforYou"
)
}

#EXAMPLE-2
temp<-30
i
f(temp>80) {
pri
nt(
"Hotout side!
")
}elseif
(temp<80&t emp>50){
pri
nt(
'Niceoutside!'
)
}elseif
(temp<50&t emp>32) {
pri
nt(
"It
scoolerout si
de!"
)
}else{
pri
nt(
"It
sr eal
l
ycol doutsi
de!"
)
}

#EXAMPLE-3
x<-c("what",
"i
s",
"t
ruth"
)
i
f("
Truth"%in%x) {
pri
nt(
"Truthisfoundthefi
rstt
ime")
}el
sei f(
"tr
uth"%in%x){
pr
int(
"tr
uthisfoundthesecondti
me"
)
}el
se{
pr
int(
"Not r
uthfound"
)
}

#EXAMPLE-4
ham <-10
cheese<-10
report<-'bl
ank'
i
f(ham >=10&cheese>=10) {
report<-"St
rongsal
esofbothi
tems"
}elseif(ham ==0&cheese==0) {
report<-"Nothi
ngsol
d!"
}else{
report<-'
Wehadsomesal es'
}
pri
nt(report
)

4.Swit
chStatement(TheRswi tchstat
ementexecut
esonest
atementf
rom mul
ti
plecondi
ti
ons.
I
tisli
keif
-el
se-i
fladderst
atement.)

#EXAMPLE-1
subj
ect<-swit
ch(3,
"
Learn",
"
RPr ogr
amming"
,
"
R",
"
Programmi
ng"
)
pr
int
(subj
ect
)

#EXAMPLE-2
number1<-30
number2<-20
oper
ator<-r
eadl
i
ne(
prompt
="Pl
easeent
eranyARI
THMETI
COPERATORYouwi
sh!
:")

swi
tch(
operat
or,
"
+"=print
(past e(
"Additionoftwonumber sis: "
, number 1+number 2))
,
"
-"=pri
nt(
past e("
Subt r
act i
onoft wonumbersi s: ",number1-number2)),
"
*"=pri
nt(paste("Multi
pl i
cati
onoft wonumber si s: "
,number1*number 2)
),
"
^"=pri
nt(paste("Exponentoft wonumber sis:", number 1^number2)),
"
/"=pri
nt(paste("
Di vi
sionoft wonumber si
s: ",number 1/number2))
,
"
%/%"=pr i
nt(paste("I
ntegerDi v
isionoft
wonumber sis:"
,number
1%/ %number
2))
,
"
%%"=pr i
nt(paste("Divisi
onoft wonumbersi s: "
, number1%%number 2))
)

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
# Dif
fer
entLoops
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

1.RepeatLoop(TheRr epeatloopisusedtoit
erat
eapar toftheprogr
am sev
eralti
mes.Ifthe
numberofi t
erationi
snotf i
xedandy oumusthav etoexecut etheloopatleastonce,itis
recommendedt ouserepeatloop.TheRrepeatl
oopisexecutedatl
eastoncebecausecondit
ion
i
scheckedafterloopbody.)

Thebasicsyntaxforcreat
ingar
epeatl
oopi
nRi
s:
repeat{
commands
if
(condit
ion){
break
}
}

#EXAMPLE-1
total<-0
number<-as. i
nteger(
readl
i
ne(prompt
="Pl
easeEnteranyi
ntegerVal
uebelow10: "
))
repeat {
total=tot
al+number
number=number+1
if(
number>10) {
break
}
}
pri
nt(paste(
"ThetotalSum ofNumber
sFrom theRepeatLoopis: "
,tot
al)
)

#EXAMPLE-2
v<-c("Hell
o","
loop"
)
cnt<-2
repeat{
pri
nt(v
)
cnt<-cnt+1
i
f(cnt>5) {
break
}
}

2.Whil
eLoop(TheRwhi l
el oopisusedtoiter
ateapartoftheprogr
am sev
eralt
imes.I
fthe
numberofit
erati
onisnotfixed,iti
srecommendedtousewhil
eloop.
)
Thebasicsyntaxforcreati
ngawhi lel
oopi
nRi s:
whil
e(test_
expressi
on){
st
atement
}

#EXAMPLE-1
v<-c("Hell
o",
"whi
l
eloop"
)
cnt<-2
whil
e( cnt<7){
pri
nt(
v)
cnt=cnt+1
}

#EXAMPLE-2
x<-0
whil
e(x<10){
cat(
'xiscur
rent
ly:
',
x)
pri
nt('
xissti
lll
essthan10,
addi
ng1t
ox'
)
#addonet ox
x<-x+1
}

#EXAMPLE-3
x<-0
whil
e(x<10){
cat('
xiscurr
entl
y:'
,x)
print
('xi
sstil
ll
esst han10,addi
ng1t ox')
#addonet ox
x<-x+1
if(
x==10){
pri
nt(
"xisequal t
o10!Terminat
ingloop"
)
}
}

#EXAMPLE-4
tot
al =0
number<-as.int
eger(
readl
i
ne(prompt
="Pl
easeEnt
eranyint
egerVal
uebelow10: "
))
while(number<=10) {
total=t
otal+number
number=number+1
}
pri
nt(past
e("
ThetotalSum ofNumber
sFrom theWhi
leLoopis: "
,tot
al)
)

3.ForLoop(TheRf orloopisusedtoi
terateapartofthepr
ogr
am sev
eralt
imes.I
fthenumber
ofit
erat
ioni
sf i
xed,itisrecommendedtousef orl
oop.)
Thebasicsyntaxforcreatingaforl
oopstatementi
nRi s:
f
or(v
alueinv ector){
stat
ement s
}
#EXAMPLE-1
v<-LETTERS[1:
4]
f
or(iinv){
pri
nt(
i)
}

#EXAMPLE-2
vec<-c(1,
2,3,
4,
5)
f
or(temp_varinvec){
pr
int
(t
emp_ var
)
}

#EXAMPLE-3
vec<-c(1,
2,3,
4,5)
f
or(iin1:l
ength(vec)
){
pri
nt(vec[
i])
}

#EXAMPLE-4
l
i<-l
i
st(
1,2,
3,4,
5)
f
or(t
emp_ vari
nli
){
pr
int(
temp_var
)
}

#EXAMPLE-5
bins<-seq(20,130,by
=10)
l
i <-l
i
st(1,
2,3,
4,5)
for(ii
n1:length(l
i
)){
pri
nt(l
i[
[i
]]
)#Remembert
ousedoubl
ebr
acket
s!
}

#EXAMPLE-6
mat<-matri
x(1:
25,
nrow=5)
f
or(num i
nmat )
{
pr
int
(num)
}

#EXAMPLE-7(Nest edforl
oops)
mat<-mat r
ix(
1:25,nrow=5)
f
or(r
owi n1:nrow( mat )
){
for(
col i
n1: ncol(mat)
){
pri
nt(paste('
Theelementatr
ow:
'
,r
ow,
'
andcol
:
',
col
,
'i
s'
,mat
[r
ow,
col
])
)
}
}

4.Br
eakStat
ement(TheRbr eakisusedt obreakl
ooporswit
chstatement.Itbreaksthecur
rent
fl
owoftheprogr
am atspeci
fi
edcondi
tion.Incaseofi
nnerl
oop,i
tbreaksonlyinnerloop.)

#EXAMPLE-1
v<-c("Hell
o",
"l
oop")
cnt<-2
repeat{
pri
nt(v
)
cnt<-cnt+1
i
f(cnt>5){
br
eak
}
}

#EXAMPLE-2
number<-10
whil
e(number>0) {
i
f(number==3) {
pri
nt(past
e("Comingoutf
rom Whi
leloopWher
enumber= "
,number
))
break
}
pri
nt(paste(
"Valuesar
e: ",number
))
number=number-1
}

#EXAMPLE-3
number<-1: 10
for(valinnumber){
if(
v al==7) {
print(
past
e("Comingoutf
rom forl
oopWher
ei= "
,val
))
break
}
pri
nt (
paste(
"Valuesar
e: ",val
))
}

5.NextStat
ement(TheRnextstat
ementisusedt
oconti
nueloop.Itcont
inuest
hecurr
entflow
oftheprogram andski
pst her
emaini
ngcodeatspeci
fi
edcondit
ion.Incaseofinnerloop,i
t
conti
nuesonlyi
nnerl
oop.)

#EXAMPLE-1
number<-1: 20
for(valinnumber){
if(
v al%%2! =0) {
print(
past
e("ODDNumber= ",
val,
"(Ski
ppedbyNextSt
atement
)")
)
nex t
}
pri
nt (
paste(
"EVENNumber= ",
val)
)
}

#EXAMPLE-2
v<-LETTERS[1:
6]
f
or(iinv){
if(
i==" D"
){
next
}
pri
nt(i
)
}

#EXAMPLE-3
number<-0
whil
e( number<=10) {
i
f(number==4| |number==7) {
pri
nt(
paste(
"Ski
ppedbytheNextSt
atement= "
,number
))
number=number+1
next
}
pri
nt(
past
e("
Val
uesar
e: "
,number
))
number=number+1
}

#=============================================================================
====
Functi
onswithR
#=============================================================================
===

AFunct
ionisaselfbl
ockofcode.
AFuncti
oncanbecal l
edasasect i
onofapr ogram t
hatiswr i
tt
enonceandcanbeexecut
ed
whenev
errequi
redintheprogram,
thusmakingcodereusabi
l
ity
.
AFunct
ionisasubprogram t
hatworksondataandproducesomeoutput
.

TypesofFuncti
ons:
TherearetwotypesofFunctions.
a)Buil
t-
inFunct
ions:Functi
onst hatarepredefi
ned.Wehav
eusedmanypr edef
inedf
unct
ionsi
nR.
b)User-Defi
ned:Functi
onst hatarecreatedaccordi
ngt
other equi
rements.
c)Recursi
veFuncti
ons: Recursi
v efunct
ionsinRmeansaf uncti
oncall
ingit
sel
f

Functi
onDef i
nit
ion
AnR f uncti
oni screat
edbyusi
ngt
hekey
wor
dfunct
ion.Thebasi
csy
ntaxofanR f
unct
ion
defi
nit
ionisasfoll
ows:

f
unct
ion_
name<-funct
ion(
arg_
1,ar
g_2,
..
.){
Funct
ionbody
}

1.Bui
lt-
inFunct i
on
#Createasequenceofnumber sf
rom 32t
o44.
pri
nt(
seq(32,44))
#Findmeanofnumber sfr
om 25to82.
pri
nt(
mean( 25:82))
#Findsum ofnumber sfr
m 41to68.
pri
nt(
sum( 41:68))

2.User-
definedFunct i
on
#EXAMPLE-1(CREATEAFUNCTI ONTOPRI
NTSQUARESOFNUMBERSI
NSEQUENCE)
new.f
unction<-f unct
ion(a){
for(i
in1: a){
b<-i ^2
pr i
nt(b)
}
}
#Callthefunct i
onnew. f
uncti
onsuppl
yi
ng6asanar
gument
.
new.f
unction(6)

#EXAMPLE-2(CREATEAFUNCTI
ONWI
THOUTANARGUMENT)
new.
funct
ion<-f unct
ion(
){
for
(iin1:5){
pri
nt(i
^2)
}
}
#Cal
l t
hefuncti
onwi
thoutsuppl
yi
nganar
gument
.
new.
funct
ion(
)

#EXAMPLE- 3(CALLI NGAFUNCTI ONWITHARGUMENTVALUES(


BYPOSI
TIONANDBYNAME)
)
new.
function<-f uncti
on(a,b,c){
result<-a*b+c
pr i
nt(
result)
}
#Cal
l thefunctionbyposi tionofarguments.
new.
function(5,
3,11)
#Cal
l thefunctionbynamesoft hearguments.
new.
function(a=11,b=5,c=3)

#EXAMPLE- 4(CREATEAFUNCTI ONWI THDEFAULTARGUMENT)


new.
functi
on<-f unct
ion(
a=3,b=6){
resul
t<-a* b
print
(resul
t)
}
#Callthefuncti
onwithoutgi
vi
nganyargument
.
new.
functi
on( )
#Callthefuncti
onwithgivi
ngnewval
uesofthear
gument
.new.
funct
ion(
9,5)

#EXAMPLE-5(LAZYEVALUATI ONOFFUNCTION.CREATEAFUNCTI
ONWI
THARGUMENTS.
)
new.
funct
ion<-funct
ion(
a,b){
pri
nt(
a^2)
pri
nt(
a)
pri
nt(
b)
}
#Eval
uatethefuncti
onwithoutsuppl
yi
ngoneoft
heargument
s.
new.
funct
ion(
6)

#EXAMPLE-6(LETUSLOOKATAN EXAMPLEWHICH WI
LLRETURN WHETHERAGI
VEN
NUMBERI SPOSITIVE,NEGATI
VEORZERO.
)
check<-f uncti
on(x){
i
f( x>0){
result<-"
Posit
ive"
}
elseif(x<0){
result<-"
Negative"
}
else{
result<-"
Zero"
}
retur
n(result
)
}

#ev al
uatethef
unct
ionwi
thar
gument
s
check(1)
check(-10)
check(0)

#EXERCISE:SUM ANDAVERAGEOF3NUMBERSUSI
NGRFUNCTI
ONS
sum.number
s<-f
uncti
on(
a,b,
c)
{
Sum =a+b+c
Av
erage=Sum/
3

pr
int
(past
e("
Sum of"
,a,
",
",b,",
",
c,"i
s=",Sum))
pr
int
(past
e("
Aver
ageof",a,"
,"
,b,",
",
c,"
is=",Av
erage)
)
}
sum.
number
s(20,
10,
70)

#Ex erci
se:Sum andAv er
ageof3Number
susi
ngRFunct
ions(I
nputShoul
dbet
akenf
rom
keyboard)
sum.number s<-funct
ion(a,
b,c)
{
a<-readl
ine(pr
ompt ="
EnteraVal
ue:"
)
b<-readl
ine(pr
ompt ="Ent
erbVal
ue:"
)
c<-readl
i
ne( pr
ompt ="
EntercVal
ue:
")

#convertcharact
eri
ntoi
nteger
a<-as.
integer
(a)
b<-as.
integer
(b)
c<-as.
integer
(c)

Sum =a+b+c
Aver
age=Sum/
3

pr
int
(past
e("
Sum of"
,a,
",
",b,",
",
c,"i
s=",Sum))
pr
int
(past
e("
Aver
ageof",a,"
,"
,b,",
",
c,"
is=",Av
erage)
)
}
sum.
number
s(a,
b,c)

3.Recursi
veFuncti
ons:
TheRPr ogramminglanguageintroducednew t
echniquecall
edasRecursi
onforsimple,and
elegantcoding.Recursi
vefunct
ionsi nR meansaf uncti
oncall
ingit
sel
f.Tounderstandthe
recursi
veprogramming,l
etusconsiderawell
know,yetsimpl
eexamplecal
l
edfact
ori
al.

#EXAMPLE-1
Number .
factor
ial
<-funct
ion(
number)
{
i
f(number==0| |number==1){
return(1)
}else{
return(number* Number .
fact
ori
al(
number-1)
)
}
}
Sum. Seri
es(6)

#EXERCI
SE:
FINDSUM OFSERI
ES1²
+2²
+3²
+….
.+N²

#Recur si
veFunctionsi
nRExampl
e
Sum. Seri
es<-functi
on(
number
)
{
i
f(number==0){
retur
n(0)
}else{
retur
n((number*number)+Sum.
Ser
ies(
number-1)
)
}
}
Sum.
Ser
ies(
5)

#=============================================================================
===
Stri
ngsinR
#=============================================================================
===

I
nR,st
ri
ngisbasi
cal
l
yanobj
ectt
hatr
epr
esent
ssequenceofcharv
alues.Anar
rayofchar
act
ers
wor
kssameasRstri
ng

#EXAMPLESOFVALI DSTRINGS
a<-'
Startandendwit
hsingl
equote'
pr
int
(a)
b<-"
Startandendwithdoubl
equotes"
pr
int
(b)
c<-"
singlequot
e'inbet
weendoublequot
es"
pr
int
(c)
d<-'
Doublequotes"i
nbetweensi
nglequot
e'
pr
int
(d)

#EXAMPLESOFI NVALIDSTRI
NGS
e<-'Mixedquotes"
pri
nt(
e)
f<-'
Singlequot
e' i
nsi
desingl
equote'
pri
nt(
f)
g<-"Doublequotes insi
dedoubl
equotes"
pri
nt(
g)

#EXAMPLE-1(CONCATENATI NGSTRINGSUSI
NGPASTE(
)FUNCTI
ON)
a<-"
Hello"
b<-'
How'
c<-"
arey ou?"
pr
int
(paste(a,
b,
c))
pr
int
(paste(a,
b,
c,sep="
-"
))
pr
int
(paste(a,
b,
c,sep="
",col
l
apse="
"))

#EXAMPLE- 2(FORMATTI NGNUMBERS&STRI NGSUSI NGFORMAT(


)FUNCTI
ON)
#Tot al numberofdi gi
tsdi splayed.Lastdi gi
troundedoff
.
result<-format (
23.123456789, digit
s=9)
print
(result)
#Di splaynumber sinsci entif
icnot ati
on.
result<-format (
c(6,13.14521) ,scientif
ic=TRUE)
print
(result)
#Themi ni
mum numberofdi gi t
st other i
ghtofthedeci
mal poi
nt.
result<-format (
23.47,nsmal l =5)
print
(result)
#For matt reatseveryt
hingasast ri
ng.
result<-format (
6)
print
(result)
#Number sarepaddedwi thbl ankint hebeginni
ngforwidth.
result<-format (
13.7,width=6)
print
(result)
#Lef tjusti
fystri
ngs.
resul
t<-f ormat
("
Hell
o",
width=8,
just
if
y="
l"
)
print
(result
)
#Just fystri
ngwit
hcenter.
resul
t<-f ormat
("
Hell
o",
width=8,
just
if
y="
c")
print
(result
)

#Exampl e-
3(Counti
ngnumberofchar
acter
sinast
ri
ng-nchar
()f
unct
ion)
resul
t<-nchar(
"Countt
henumberofchar
acter
s")
print
(resul
t)

#Exampl e-4(Changingthecase-t
oupper
()&t
olower
()f
unct
ions)
#Changi ngt oUppercase.
resul
t<-t oupper(
"Changi
ngToUpper")
print
(result
)
#Changi ngt olowercase.
resul
t<-t ol
ower (
"Changi
ngToLower")
print
(result
)

#Exampl e-
5(Extract
ingpartsofastri
ng-subst
ri
ng(
)funct
ion)
#Ext r
actcharact
ersfrom 5thto7thposit
ion.
resul
t<-substri
ng("
Extract
",5,7)
print
(resul
t)

#=============================================================================
===
DataReshapinginR
#=============================================================================
===

#- -
--
---
--
--
--
---
--
--
---
--
--
---
--
--
---
--
---
---
--
--
----
--
---
--
--
--
--
--
--
--
--
--
--
-
#Dat aReshapi ng
#Joi ni
ngCol umnsandRowsi naDat aFrame
#Cr eatevectorobjects.
ci
ty<-c( "
Tampa" ,"
Seat t
le",
"Hartf
or d",
"Denver"
)
state<-c("FL",
"WA","CT","
CO" )
zi
pcode<-c( 33602,98104,06161, 80294)
#Combi neabov ethreev ectorsintoonedat aframe.
addresses<-cbi nd(cit
y,st
ate,zi
pcode)

#Pr i
ntaheader .
cat( "
####TheFi rstdataframe\n")
#Pr i
ntthedat af rame.
print(addr esses)
#Cr eateanot herdat aframewi thsimil
arcol
umns
new. addr ess<-dat a.fr
ame(
ci
ty=c( "Lowr y",
"Charlot
te"),
stat e=c( "CO" ,"
FL" )
,
zi
pcode=c( "80230" ,"
33949"),
stringsAsFact ors=FALSE
)
#Pr i
ntaheader .
cat( "
###TheSeconddat aframe\n")
#Pr i
ntthedat af rame.
print(new. addr ess)
#Combi ner owsf orm boththedat afr
ames.
al
l.
addresses<-r bi
nd(addr
esses,new.
addr
ess)
#Printaheader .
cat
("###Thecombi neddataframe\n"
)
#Printtheresult
.
pri
nt(al
l
.addresses)

#Mer
gingDat
aFr
ames

#Exampl
e-1
df
1=data.
fr
ame(
Cust
omer
Id=c(
1:6)
,Product=c(
rep(
"Toaster
",3),
rep("
Radi
o",
3)))
df
2=data.
fr
ame(
Cust
omer
Id=c(
2,4,6)
,State=c(
rep(
"Alabama",2),r
ep("
Ohi
o",1)
))

#Innerjoi
n:mer ge(
df1,df2)willworkf
ortheseexampl
esbecauseRaut omatical
lyjoi
nst he
fr
amesbycommonv ar
iabl
enames, buty
ouwouldmostli
kelywanttospecifymer ge(
df1,df2,by
="CustomerI
d")t
omakesur et
hatyouwerematchi
ngononlythefi
eldsy oudesi
red.Youcanal so
usetheby.xandby .
yparametersift
hematchi
ngvari
abl
eshav edi
fferentnamesi nthediff
erent
datafr
ames.

i
nner.
joi
n<-merge(
df1,
df2)
pr
int
(i
nner.
joi
n)

#Outerj
oin:
out
er.
joi
n<-mer
ge(
x=df
1,y=df
2,by="
Cust
omer
Id"
,al
l=TRUE)

#Leftout
er:
l
eft
.j
oin<-merge(
x=df
1,y=df
2,by="
Cust
omer
Id"
,al
l
.x=TRUE)

#Rightout
er:
r
ight.
joi
n<-merge(
x=df
1,y=df
2,by="
Cust
omer
Id"
,al
l
.y=TRUE)

#Crossjoi
n:
cr
oss.j
oin<-mer
ge(
x=df
1,y=df
2,by=NULL)

l
ibrary(
MASS)
mer ged.
Pima<-mer ge(
x=Pi
ma.
te,
y=Pi
ma.
tr
,
by.x=c("
bp","bmi")
,
by.y=c("
bp","bmi")
)
pri
nt(merged. Pi
ma)
nrow(mer ged.Pi
ma)

#MeltingandCasting
l
ibr
ary(MASS)
pri
nt(
ships)
i
nstal
l.
packages(
"reshape"
)

l
ibr
ary(r
eshape)
#Mel tt
heDat a
molten.
ships<-melt
(shi
ps,
id=c(
"t
ype"
,"
year
"))
pri
nt(mol
ten.shi
ps)

#Castt heMoltenData
recasted.ship<-cast
(mol
ten.
shi
ps,
type+y
ear
~var
iabl
e,
sum)
print
(recasted.
ship)
=================================================

Basi
cal
l
y ,y
ou"mel
t"dat
asothateachr
owi sauniquei
d-var
iabl
ecombinati
on.Theny
ou"
cast
"
themel
teddat
aint
oanyshapeyouwoul
dli
ke.Her
eisaverysimpleexampl
e.

my
dat
a

i
d t
imex1 x2
1 1 5 6
1 2 3 5
2 1 6 1
2 2 2 4

#exampleofmel tfunct
ion
l
ibr
ary(
reshape)
mdata<-melt(mydata,i
d=c("
id"
,"
ti
me"
))

newdat
a

i
d t
imev
ari
abl
eval
ue
1 1 x1 5
1 2 x1 3
2 1 x1 6
2 2 x1 2
1 1 x2 6
1 2 x2 5
2 1 x2 1
2 2 x2 4

#castthemelt
eddat
a
#cast(
data,
for
mula,
funct
ion)
subj
means<-cast
(mdata,
id~var
iabl
e,mean)
ti
memeans<-cast
(mdata,t
ime~vari
abl
e,mean)

subj
means

i
d x1 x2
1 4 5.5
2 4 2.5

t
imemeans

ti
mex1 x2
1 5. 5 3.
5
2 2. 5 4.
5
Therei
smuchmoret
haty
oucandowi
tht
hemel
t()andcast
()f
unct
ions.Seet
hedocument
ati
on
formor
edetai
l
s.

===================================================

AnI nt
roducti
ontor
eshape2
reshape2isanRpackagewr i
tt
enbyHadl
eyWi
ckham t
hatmakesi
teasyt
otr
ansf
orm dat
a
betweenwi deandl
ongformat
s.
Whatmakesdat
awideorlong?
Wi
dedatahasacol
umnf oreachv
ari
abl
e.Forexampl
e,t
hisi
swi
de-
for
matdat
a:

# ozone wind temp


#123.
6211.
62365.
55
#229.
4410.
26779.
10
#359.
12 8.
94283.
90
#459.
96 8.
79483.
97

Andt
hisi
slong-
for
matdat
a:

# v
ariable v
alue
#1 ozone23.615
#2 ozone29.444
#3 ozone59.115
#4 ozone59.962
#5 wind11.623
#6 wind10.267
#7 wind 8.942
#8 wind 8.794
#9 temp65.548
#10 t
emp79.100
#11 t
emp83.903
#12 t
emp83.968

Long-f
or matdatahasacol umnforpossiblev ari
abl
et ypesandacolumnfort
hev al
uesofthose
vari
ables.Long-formatdat aisn’
tnecessaril
yonl yt wocolumns.Forexample,wemi ghthave
ozonemeasur ementsf oreachdayofthey ear .Inthatcase,wecouldhav
eanothercolumnfor
day.Inot herwords,therearedif
fer
entlevelsof“ l
ongness”.Theul
ti
mateshapeyouwantt oget
yourdatai nt
owilldependonwhaty ouaredoi ngwi t
hi t
.

Itt
urnsoutt hatyouneedwi de-f
ormatdataforsomet ypesofdataanalysi
sandlong-for
matdata
forothers.Inr
eali
ty,youneedl ong-f
ormatdatamuchmor ecommonl ythanwide-
formatdat
a.For
exampl e,ggpl
ot2requireslong-for
matdata( t
echnicall
ytidydat
a),plyrr
equir
eslong-f
ormatdat
a,
andmostmodel l
ingf unct
ions( suchasl m(),gl
m( ),andgam( ))requir
elong-f
ormatdata.But
peopleof t
enfinditeasiert
or ecordthei
rdatainwidef ormat.

Ther
eshape2package

r
eshape2i
sbasedar
oundt
wokeyf
unct
ions:
mel
tandcast
:

mel
ttakeswi
de-
for
matdat
aandmel
tsi
tint
olong-
for
matdat
a.

castt
akesl
ong-
for
matdat
aandcast
siti
ntowi
de-
for
matdat
a.

Thi
nkofwor ki
ngwit
hmetal
:ify
oumel
tmet
al,i
tdr
ipsandbecomesl
ong.I
fyoucasti
tint
oa
mould,
itbecomeswi
de.

Wide-t
olong-
for
matdata:themel
tfuncti
on
Forthi
sexamplewe’l
lworkwitht
heai r
quali
tydat
asett
hati
sbuil
tint
oR.Fir
stwe’l
lchanget
he
col
umnnamest olowercasetomakethem easi
ert
oworkwit
h.Thenwe’l
ll
ookatt
hedata:

names(air
quali
ty)<-t
olower
(names(
air
qual
i
ty)
)
head(
airqual
i
ty)
# ozonesol
ar.
rwi
ndtempmont
hday
#1 41 190 7.
4 67 5 1
#2 36 118 8.
0 72 5 2
#3 12 14912.
6 74 5 3
#4 18 31311.
5 62 5 4
#5 NA NA14.3 56 5 5
#6 28 NA14.
9 66 5 6

Whathappensi
fwer
unt
hef
unct
ionmel
twi
thal
lthedef
aul
tar
gumentv
alues?

aql<-mel t(
air
quali
ty)#[
a]i
r[q]
ual
i
ty[
l]
ongf
ormat
head(aql)
# v ariabl
ev al
ue
#1 ozone 41
#2 ozone 36
#3 ozone 12
#4 ozone 18
#5 ozone NA
#6 ozone 28

t
ail
(aql
)
# var
iableval
ue
#913 day 25
#914 day 26
#915 day 27
#916 day 28
#917 day 29
#918 day 30

Bydefaul
t,melthasassumedthatallcol
umnswi t
hnumer icvaluesarevar
iabl
eswi t
hv alues.
Oftent
hisiswhatyouwant.Maybeherewewantt oknowt hev al
uesofozone,sol
ar.
r,wind,and
tempforeachmonthandday.Wecandot hatwit
hmel tbytel
li
ngi tt
hatwewantmont handday
tobe“I
Dv ari
abl
es”
.IDvar
iabl
esaret
hev ar
iablest
hatident
if
yindiv
idualr
owsofdata.

aql<-melt
(ai
rquali
ty,i
d.v
ars=c(
"mont
h",
"day
"))
head(aql
)
# mont hdayv ar
iableval
ue
#1 5 1 ozone 41
#2 5 2 ozone 36
#3 5 3 ozone 12
#4 5 4 ozone 18
#5 5 5 ozone NA
#6 5 6 ozone 28

Whati
fwewantedtocont
rolt
hecol
umnnamesi
nourl
ong-
for
matdat
a?mel
tlet
sussett
hose
t
ooall
inonest
ep:

aql<-mel t
(ai
rquali
ty,
id.v
ars=c( "
month"
,"day
"),
v
ariable.
name=" cl
imate_var
iabl
e",
v
alue.name=" cl
i
mat e_val
ue")

head(
aql
)
# mont hdaycl
i
mat
e_v
ari
abl
ecli
mat
e_v
alue
#1 5 1 ozone 41
#2 5 2 ozone 36
#3 5 3 ozone 12
#4 5 4 ozone 18
#5 5 5 ozone NA
#6 5 6 ozone 28

Long-t
owide-formatdat
a:thecastf unct
ions
Whereasgoingfrom wi
de-t
ol ong-formatdataisprett
yst r
aight
for
war
d,goi
ngfr
om l
ong-t
owi
de-
for
matdatacant akeabitmor ethought.Itusual
l
yi nvol
vessomeheadscrat
chi
ngandsomet
ri
al
anderr
orforallbutt
hesimplestcases.Let’sgothroughsomeexamples.

I
nr eshape2 t
her
e are mul
ti
ple castfuncti
ons.Since y
ou wil
lmostcommonlywork wi
th
data.fr
ameobject
s,we’
ll
expl
orethedcastfuncti
on.(
Thereisal
soacastt
oret
urnav
ect
or,
matr
ix,
orarray.)

Let
’stakethelong-f
ormatai
rqual
i
tydat
aandcastiti
ntosomedif
ferentwi
def
ormat
s.Tost
art
wit
h,we’l
lrecov
erthesameformatwestar
tedwi
thandcompar
ethetwo.

dcastusesaformulatodescr
ibetheshapeofthedat a.Theargument
sont heleftr
efert
ot heID
vari
abl
esandt heargumentsont heri
ghtrefertot hemeasuredvari
abl
es.Comi ngupwi ththe
ri
ghtformul
acant akesomet ri
alanderroratfi
rst.So,ifyou’
rest
uckdon’tfeelbadaboutjust
experi
menti
ngwithformul
as.Therear
eusuallyonlysomanyway sy
oucanwr i
tetheformula.

Here,weneedt otelldcastthatmonthanddayar etheI


Dv ar
iabl
es(wewantacolumnforeach)
andt hatvari
abledescri
best hemeasuredv ar
iabl
es.Si
ncethereisonl
yoneremaini
ngcolumn,
dcastwillfi
gureoutthatitcontai
nsthevaluesthemsel
ves.Wecouldexpl
i
cit
lydecl
aret
hiswith
val
ue.var.(
Andinsomecasesi twil
lbenecessaryt
odoso.)

aql<-mel t
(ai
rquali
ty,i
d.var s=c("month",
"day"
))
aqw<-dcast (aql
,mont h+day~v ari
able)
head(aqw)
# mont hdayozonesol ar.rwindt
emp
#1 5 1 41 190 7.4 67
#2 5 2 36 118 8.0 72
#3 5 3 12 14912.6 74
#4 5 4 18 31311.5 62
#5 5 5 NA NA14.3 56
#6 5 6 28 NA14.9 66
head(ai
rquali
ty)#or i
ginal data
# ozonesol ar
.rwindt empmont hday
#1 41 190 7. 4 67 5 1
#2 36 118 8. 0 72 5 2
#3 12 14912. 6 74 5 3
#4 18 31311. 5 62 5 4
#5 NA NA14. 3 56 5 5
#6 28 NA14. 9 66 5 6
So,besidesre-arr
angingt hecolumns, we’ver
ecover
edouror
igi
nal
dat
a.

I
fiti
sn’
tcl
eart
oyouwhatj
usthappenedt
her
e,t
henhav
eal
ookatt
hisi
l
lust
rat
ion:

Figur
e1:Ani ll
ustr
ationofthedcastf
unction.Thebl ueshadingindicat
esIDvari
abl
esthatwe
wanttor epr
esentindiv
idualr
ows.Theredshadi ngrepresent
sv ar
iablenamesthatwewantto
swingintocol
umnnames.Thegr eyshadingrepresentsthedatavaluesthatwewanttof
il
lint
he
cell
swith.
Oneconfusi
ng“mist
ake”youmightmakei
scast
ingadataseti
nwhichthereismor
ethanone
val
ueperdatacel
l
.Forexampl
e,t
hist
imewewon’
tincl
udedayasanI
Dv ar
iabl
e:

dcast
(aql
,mont h~vari
abl
e)
# mont hozonesolar
.rwi
ndtemp
#1 5 31 31 31 31
#2 6 30 30 30 30
#3 7 31 31 31 31
#4 8 31 31 31 31
#5 9 30 30 30 30
Wheny ourunthisi
nR,you’
l
lnoti
cet
hewar
ningmessage:

#Aggr egat i
onf unctionmissing: defaul
ti
ngt ol ength
Andi fyoul ookatt heoutput,thecel lsaref il
ledwi ththenumberofdatar owsf oreachmont h-
cli
mat ecombi nati
on.Thenumber swe’reseei ngar ethenumberofday sr
ecordedi neachmont h.
Wheny oucasty ourdataandt her earemul tipl
ev aluespercell
,youal
soneedt otelldcasthowto
aggregatet hedat a.Forexampl e,may bey ouwantt ot aket
hemean,ort hemedi an,orthesum.
Let’
st r
yt hel astexample,butt hist i
mewe’ lltaket hemeanoft hecl
imatevalues.We’llal
sopass
theopt i
onna. r
m =TRUEt hrought he..
.argumentt or emoveNAv al
ues.(
The. .
.let’
sy oupasson
addit
ional argument stoyourf un.aggregatef unction,heremean.)

dcast(aql,month~v ari
able,
fun.aggregat
e=mean,
na.rm =TRUE)
# mont hozonesol ar
.r wi nd t emp
#1 523.62 181. 311.62365.55
#2 629.44 190. 210.26779.10
#3 759.12 216. 5 8.94283. 90
#4 859.96 171. 9 8.79483. 97
#5 931.45 167. 410.18076.90
Unlikemel t
,therearesomeot herfancythingsyoucandowithdcastthatI

m notcover
ingher
e.
It
’swor threadingthehelpf i
l
e? dcast.Forexample,y
oucancomput esummariesforr
owsand
columns, subsetthecolumns,andf i
l
l i
nmissingcel
lsi
nonecal
ltodcast.

Addi
ti
onalhelp
Readthepackagehel
p:hel
p(package="
reshape2"
)

#=============================================================================
===
Dat
aInputandOut putwit
hR
#=============================================================================
===

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
# CSVFiles
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--

#Pl
easecopyt
hebel
owdat
aandpast
eiti
nnot
epad,
andsav
eitasempl
oyee.
csv

EMPNO,ENAME,JOB,MGR, SAL,COMM,DEPTNO
7369,
SMITH,
CLERK,7902,800,,
20
7499,
ALLEN,
SALESMAN, 7698,1600,
300,
30
7521,
WARD,SALESMAN,7698, 1250,
500,
30
7566,
JONES,MANAGER, 7839,2975,,
20
7654,
MARTIN,SALESMAN, 7698,1250,1400,
30
7698,
BLAKE,MANAGER, 7839,2850,
,30
7782,
CLARK,MANAGER, 7839,2450,
,10
7788,
SCOTT,ANALYST,7566,3000,,
20
7839,
KING,
PRESIDENT,,5000,,
10
7844,
TURNER,SALESMAN, 7698,1500,0,
30
7876,
ADAMS,CLERK,7788,1100,,
20
7900,
JAMES,CLERK,7698,950,,
30
7902,
FORD,ANALYST,7566,3000,,
20
7934,
MILLER,
CLERK,7782,1300,,
10

f
il
es=li
st.
fil
es(pattern=".
csv")
f
or(i
in1:lengt
h( f
il
es) )
{
fi
lename=f i
les[i]
data=read.csv (
fil
e=f i
lename,
header=T)
assign(x=f i
lename, value=dat
a)
}

gr
oup.by<-aggr
egat
e(empl
oyee$SAL~empl
oyee$JOB,
empl
oyee,
sum)
pr
int
(group.
by)

CSV standsf orcommasepar at


edv ariabl
eandi tsoneoft hemostcommonway swe' llbe
worki
ngwi thdatat hroughoutt hiscourse.Thebasicf ormatofacsvf i
leist hefi
rstli
neindicati
ng
thecolumnnamesandt her estoftherows/li
nesbeingdat apointssepar at
edbycommas.Oneof
themostbasi cway stor eadi ncsvf i
l
esi nRi stouser ead.csv(
)whi chi sbuilt
-i
nt oR.Lat eron
we'l
llearnaboutf readwhi chwi l
lbeabi tfasterandmor econv enient,butitsimpor t
antt o
understandally
ouropt i
ons!
Whenusi ngread.csv (
)y oullneedtoeitherpassint heentirepathoft hef i
l
eorhav ethefil
ebei n
thesamedi rectoryasy ourRscr i
pt.Makesur etoaccountf orpossiblespacesi nthefil
epat h
name, youmayneedt ousebacksl ashest oaccountforthis.Thi
sisof tenapoi ntofconfusionfor
peoplenewt oprogr ammi ng, somakesur ey
ouunder standtheabov ebef orecontinui
ng!

r
ead.
csv
(fi
le,
header=TRUE,sep="
,"
,quote="\
"",
dec=".
",f
il
l=TRUE,comment.
char=""
,..
.)

r
ead.
csv
2(fi
le,
header=TRUE,
sep="
;"
,quote="
\""
,
dec=",
",f
il
l=TRUE,
comment.
char="
",.
..
)

r
ead.
del
i
m( fi
l
e,header=TRUE,sep="
\t"
,quot
e="\""
,
dec="."
,fi
ll
=TRUE,comment.
char=""
,..
.)

r
ead.
del
i
m2( f
il
e,header=TRUE,sep="
\t"
,quot
e=" \
"",
dec=","
,fi
l
l=TRUE,comment.
char="",
..
.)

#RReadcsvFi l
efrom CurrentWor ki
ngDi
rect
ory
#LocatetheCurrentWorkingDirector
y
getwd()
set
wd( "
D:\\
RProgrammi ng\\Data"
)
mydata<-read.
csv("
empl oyee.
csv")
pri
nt(
my dat
a)

#I
fyoudon'
thav
ethenamesoft
hev
ari
abl
esi
nthef
ir
str
ow
employ
ee<-read.
csv
("empl
oyee_
csv
.csv
",header
=TRUE)
pr
int
(empl
oyee)

#Ifyouwantt osetanyval
uetoamissingval
ue
employee<-read.
csv("
employ
ee_csv
.csv"
,header
=TRUE,
na.
str
ings="
."
)
pr
int
(employee)

#Ifyouwantt osetmult
ipl
evaluestomissingval
ues
employee<-read.
csv(
"employ
ee_ csv
.csv
",header
=TRUE,
na.
str
ings= c(
"A",
"B")
)
pr
int
(employee)

#Accessingall
theEl
ements(
Rows)Pr
esenti
nthe3r
dColumn(i
.e.
,Occupat
ion)
#IndexValues:1=empno,
2=ename,3=job,4=mgr,5=Sal
ary
,6=comm and7=dept
no
employee[
[5]
]

#Accessi
ngal
ltheEl
ement
s(Rows)Pr
esenti
ntheJOB(
Col
umn)
empl
oyee$JOB

#Accessi
ngEl
ementat4t
hRowand3r
dCol
umn
empl
oyee[
4,3]

#Accessi
ngIt
em at1st,2nd4t
hRowsand4t
h,5t
h,6t
h,7t
hCol
umns
empl
oyee[
c(1,
2,4),c(
4:7)]

#Itr
etur
nstheMaximum Val
uewithi
ntheSALCol
umn
maxi
mum. sal
ary<-max(
empl
oyee$SAL)
pr
int
(maximum.sal
ary
)

#Itr
eturnstheMi ni
mum Valuewithi
ntheSALCol
umn
mini
mum. sales<-min(
employee$SAL)
pr
int
(minimum. sal
es)

#Itwil
lcal
culateandret
urnstheSALCol
umnMeanVal
ue
mean.sal
es<-mean( empl
oyee$SAL)
pr
int
(mean.sales)

#Itreturnsall
therecor
ds,whoseJOBisequal
toSALESMAN
subdata<-subset(
employee,JOB=="SALESMAN"
)
pri
nt(subdata)

#Itreturnsallt
herecor
ds,whoseEducat
ioni
sequalt
oBachel
orsandYear
lyI
ncome>70000
part
ialdata<-subset
(empl
oyee,JOB=="SALESMAN"&SAL>1000)
pri
nt(parti
aldat
a)

print(empl oy ee)
typeof (empl oy ee)
cl
ass( empl oy ee)
names( empl oyee)
l
engt h( empl oy ee)
nrow( empl oy ee)
ncol (
empl oy ee)
dim( empl oy ee)
str(empl oyee)
summar y(empl oy
ee)
Str
ingsAsFactori
nRReadcsvf uncti
on:
Ifyourcsvfil
econt ai
nsbothcharacterandnumeri
cv ar
iabl
esthent
hechar
acterv ar
iabl
esget
automati
call
yconv ert
edtothefactorstype.Topr
eventthisaut
omat
icconv
ersion,wehav eto
specif
ystri
ngsAsFactors=FALSEexplici
tl
y.

#LocatetheCur r
entWorki
ngDir
ectory
getwd(
)
employee<-read.
csv(
"employ
ee_csv.
csv"
,TRUE,
sep="
,"
,st
ri
ngsAsFact
ors=FALSE)
str
(employee)

employee<-r
ead.
csv
("empl
oyee_
csv
.csv
",TRUE,
sep="
,"
,st
ri
ngsAsFact
ors=TRUE)
st
r(employ
ee)

==============================================================================
===
RReadt abl
eFunction

Theread.t
ablefunct
ionisv er
yusefultoi
mportthedat
afrom textf
il
esfr
om fi
l
esystem &URLs,
andstorethedatainaDat aFrame.Inthi
sarti
clewewil
lshowy ou,Howtousethi
sRr eadt
abl
e
funct
ion,
howt omani pul
atethedat
ai nRProgr
ammingwithexample

r
ead.
tabl
e(f
il
e,header=FALSE, sep=" ",quote=" \"'
",
dec=" ."
,numer als=c( "allow.loss","warn.l
oss","no.l
oss"
),
row.names, col.
names, as.i
s=! str
ingsAsFact ors,
na.str
ings=" NA", col
Cl asses=NA, nrows=- 1,
skip=0, check.names=TRUE, fi
l
l=! blank.
li
nes.skip,
stri
p.whit
e=FALSE, blank. l
i
nes.skip=TRUE,
comment .
char=" #" ,
all
owEscapes=FALSE, fl
ush=FALSE,
stri
ngsAsFact ors=def aul t.
stri
ngsAsFact ors(
) ,
fi
leEncoding=" ",encodi ng=" unknown" ,t
ext,skipNul=FALSE)

Argument s
1.f i
le
thenameoft hef i
lewhicht hedat aaret obereadf r
om.Eachr owoft het ableappear sasonel ine
oft hef i
le.Ifitdoesnotcont ai
nanabsol ut
epat h,t
hef i
l
enamei srelativet ot hecurrentwor king
directory,get wd().Til
de-expansioni sperformedwher esuppor ted.Thiscanbeacompr essedf il
e
(seef il
e).
Alternatively,fil
ecanbear eadablet ext-
modeconnect ion( whichwi llbeopenedf orr eadingi f
necessar y,andi fsocl osed( andhencedest royed)att heendoft hef unct i
oncal l)
.( I
fst din()is
used,t hepr ompt sforli
nesmaybesomewhatconf using.Termi nateinputwi thabl ankl ineoran
EOFsi gnal,Ct r
l-
DonUni xandCt rl
-ZonWi ndows.Anypushbackonst din()wi llbeclearedbef ore
return.)
fi
lecanal sobeacompl eteURL.( Fort hesuppor t
edURLschemes,seet he‘ URLs’sect i
onoft he
helpf orur l
.)
2.header
alogi calv alueindicati
ngwhet hert hefil
econt ai
nst henamesoft hev ariablesasi tsfirstline.If
missi ng,thev aluei sdeterminedf rom thefil
ef or
mat :headeri ssett oTRUEi fandonl yift hef i
rst
rowcont ainsonef ewerf i
eldthant henumberofcol umns.
3.sep
thef i
eldsepar atorcharacter.Valuesoneachl i
neoft hef i
learesepar atedbyt hischaracter.Ifsep
=" "(thedef aultforread.table)thesepar atoris‘whitespace’ ,thatisoneormor espaces,t abs,
newl i
nesorcar r
iagereturns.
4.quot e
thesetofquot ingchar acters.Todi sablequot i
ngal t
oget her,usequot e=" ".Seescanf ort he
behav iouronquot esembeddedi nquot es.Quot ingi sonl yconsi der edf orcol umnsr eadas
char act er ,whi chi sal loft hem unl esscol Cl assesi sspeci fied.
5.dec
thechar act erusedi nt hef ilefordeci mal poi nts.
6.numer als
stringi ndi cat inghow t oconv ertnumber swhoseconv ersi ont odoubl epr eci sionwoul dl ose
accur acy ,seet ype. conv ert.Canbeabbr ev iated.
7.r ow. names
av ect orofr ow names.Thi scanbeav ect orgi v i
ngt heact ualr ow names,orasi nglenumber
givingt hecol umnoft het ablewhi chcont ai nst her ownames, orchar act erst ringgi v i
ngt hename
oft het abl ecol umncont ainingt her ownames.
Ifther ei saheaderandt hef irstr ow cont ainsonef ewerf iel dthant henumberofcol umns,t he
fir
stcol umni nt hei nputi susedf ort her ownames.Ot her wi sei frow. namesi smi ssing, ther ows
arenumber ed.
Usi ngr ow. names=NULLf or cesr ow number ing.Mi ssingorNULLr ow. namesgener ater ow
namest hatar econsi der edt obe‘ automat ic’(andnotpr eser vedbyas. mat rix).
8.col .
names
av ect orofopt ionalnamesf ort hev ari
abl es.Thedef aul tist ouse" V"f ollowedbyt hecol umn
number .
9.as. i
s
thedef aul tbehav iorofr ead. t
abl ei st oconv er tchar act erv ar iables( whi char enotconv ertedt o
l
ogi cal , numer icorcompl ex)t of actors.Thev ar i
ableas. iscont rolst heconv ersionofcol umnsnot
other wi sespeci fiedbycol Classes.I tsv aluei sei therav ect orofl ogi cals( valuesar er ecy cledif
necessar y ),orav ectorofnumer i
corchar act eri ndi ceswhi chspeci f ywhi chcol umnsshoul dnot
beconv er tedt of act ors.
Not e:t o suppr ess al lconv ersi ons i ncl udi ng t hose ofnumer ic col umns,setcol Cl asses =
"char act er ".
Not et hatas. isi sspeci f
iedpercol umn( notperv ariabl e)andsoi ncludest hecol umnofr ow
names( i
fany )andanycol umnst obeski pped.
10.na. st rings
achar act erv ect orofst ringswhi char et obei nt erpret edasNAv alues.Bl ankf ieldsar eal so
consi der edt obemi ssingv aluesi nlogi cal ,integer ,numer icandcompl exf ields.
11.col Cl asses
char act er .Av ectorofcl assest obeassumedf ort hecol umns.Recy cledasnecessar y.I fnamed
andshor tert hanr equi red,namesar emat chedt ot hecol umnnameswi thunspeci f i
edv aluesar e
takent obeNA.
Possi bl ev aluesar eNA( thedef ault,whent y pe.conv erti sused) ,"NULL"( whent hecol umni s
skipped) ,oneoft heat omi cv ect orcl asses( logi cal,i nteger ,numer i
c,compl ex,char acter,raw) ,or
"fact or" ,"Dat e"or" POSI Xct "
.Ot her wiset her eneedst obeanasmet hod( from packagemet hods)
forconv er sionf r
om " char act er"t ot hespeci fiedf ormal class.
Not et hatcol Cl assesi sspeci fiedpercol umn( notperv ariabl e)andsoi ncludest hecol umnofr ow
names( i
fany ).
12.nr ows
i
nt eger : themaxi mum numberofr owst or eadi n.Negat iveandot heri nv alidv aluesar eignor ed.
13.ski p
i
nt eger : thenumberofl i
nesoft hedat af ilet oski pbef orebegi nningt or eaddat a.
14.check. names
l
ogi cal .I fTRUEt hent henamesoft hev ar i
abl esi nt hedat af ramear echeckedt oensur et hatt hey
aresy nt act ical l
yv alidv ariablenames.I fnecessar yt heyar eadj ust ed( bymake. names)sot hat
theyar e, andal sot oensur ethatt herear enodupl i
cat es.
15.f ill
l
ogi cal .I fTRUEt heni ncaset her owshav eunequall engt h,bl ankf ieldsar ei mpl icitlyadded.See

Det ails’.
17.st rip. whi te
l
ogi cal .Usedonl ywhensephasbeenspeci fied,andal l
owst hest r
ippi ngofl eadi ngandt rail
ing
whitespacef rom unquot edchar act erf i
el ds( numer icfiel
dsar eal way sstripped) .Seescanf or
furt
herdet ails( i
ncludi ngt heexactmeani ngof‘ whi tespace’),remember i
ngt hatthecol umnsmay
i
ncludet her ownames.
18.bl ank. l
ines. skip
l
ogical : ifTRUEbl ankl inesi nt heinputar ei gnor ed.
19.comment .char
char act er:achar act erv ectorofl engt honecont ainingasi nglechar act eroranempt yst r
ing.Use
""tot ur nof fthei nter pretationofcomment sal t
oget her
.
20.al lowEscapes
l
ogical .Shoul dC- st yleescapessuchas\ nbepr ocessedorr eadv er batim( thedef aul t
)?Not et
hat
i
fnotwi thinquot est hesecoul dbei nt erpr etedasadel i
mi t
er( butnotasacommentchar act
er )
.
Formor edet ail
sseescan.
21.f l
ush
l
ogical :i fTRUE, scanwi l
lf l
usht ot heendoft hel i
neaf terreadingt hel astoft hef i
el dsr equested.
Thisal l
owsput ti
ngcomment saf t
ert hel astf iel
d.
22.st ringsAsFact ors
l
ogical : shoul dchar acterv ect orsbeconv er tedt of actors?Not et hatt hi
si sov erri
ddenbyas. isand
colCl asses, bothofwhi chal lowf i
nercont rol.
23.f i
leEncodi ng
char act erst ring:ifnon- empt ydecl ar est heencodi ngusedonaf i
le( notaconnect ion)sot he
char act erdat acanber e-encoded.Seet he‘ Encodi ng’sect i
onoft hehel pf orf i
le,t he‘RDat a
Impor t/Expor tManual ’and‘ Not e’.
24.encodi ng
encodi ngt obeassumedf orinputst ri
ngs.I tisusedt omar kchar acterst ri
ngsasknownt obei n
Latin-1orUTF- 8( seeEncodi ng) :iti snotusedt or e-encodet hei nput ,butal lowsRt ohandl e
encodedst r
ingsi nt hei rnat i
veencodi ng( ifoneoft hoset wo).See‘ Val ue’ and‘ Note’.
25.t ext
char act erst ri
ng:i ff i
lei snotsuppl iedandt hisi s,thendat aar er eadf rom thev alueoft extvi
aa
textconnect ion.Not icethatal i
teral stringcanbeusedt oinclude( smal l
)dat aset swi thinRcode.
26.ski pNul
l
ogical : shoul dnul sbeski pped?
..
.
Furtherar gument st obepassedt or ead. t
abl e.

#RReadt abl
ef unct
iontoreadTextFil
efrom Cur
rentWorki
ngDi
rect
ory
#ToLocat et
heCur r
entWor kingDir
ect
ory
getwd()
set
wd( "
D:\
\RProgrammi ng\\Data"
)
Company .
employees<-read.t
able(
"empl
oyee.
txt"
,TRUE,
sep=","
)
pri
nt(
Company .
employees)

RReadt ableFunction–t esti


ngar gument s
#RReadt ablefuncti
on-Test ingargument
#ToLocat et heCurrentWor kingDirector
y
getwd()
set
wd( "
D:\\RPr ogr
ammi ng\ \Data")
employees<-r ead.
table(
"empl oyee_update.
txt
",TRUE,sep=",
",quot
e="
\""
,
na.st
rings=TRUE, stri
p.whi
te=TRUE,
comment .
char="$",
blank.
li
nes.
ski
p=TRUE)
pri
nt(
empl oyees)

I
nthi
sexamplewewil
lshowy ou,
Howt oreadNAr
ecor
ds,
escapet
hebl
ankl
i
nes,
andcomment
l
i
neswhil
ereadi
ngdat
afrom t
hetextf
il
e.

#al
l
owEscapes:ABool
eanv
aluet
hati
ndi
cat
eswhet
hery
ouwantt
oal
l
owt
heescapes(
suchas
\nfornewl ine)ornot .
#strip.
whi te:Ifthesepar gumenti snotequalt o“”t heny oumayuset hi
sBool eanvaluetotri
m
theextr
al eadi ng&t ail
ingwhi tespacesf r
om thechar act
erf i
eld.
#comment .char:Ifthereareanycommentl i
nesiny ourtextfil
et henyoucanuset hisargument
toignoret hosel ines.Her e,Youhav etodescr i
bethesi nglespeci alcharacterthatyouusedt o
commentt hel i
ne.Forexampl e,ify ourtextfil
econt ainscommentst arti
ngwi th$t henuse
comment .char=“ $”toski pthiscommentl i
nefrom reading.
#blank.l
ines. ski
p:ABool eanv aluethatspecifi
eswhet hery ouwantt oskip/ignoretheblankli
nes
ornot.
#na.str
ings: Achar act
erv ectorspecifyi
ngv al
uesthatshoul dber eadasNA

#RReadtabl
efunct
ion-Testi
ngar
gument
#ToLocat
etheCurr
entWor ki
ngDi
rect
ory

getwd()
employeeNames <- c( "
Empl oyee_ID", " Fir
st Name" , " Last Name"
, "
Educat
ion"
,
"Pr
ofessi
on","
Salar
y",
"Sal
es")
employees<-read.
table(
"EmployeeSales.t
xt"
, TRUE, sep=" ,
",quote="\
"",
na.st
rings=TRUE, stri
p.whit
e=TRUE, ski
p=3,
as.i
s=c( TRUE, TRUE, FALSE, FALSE, TRUE),
col.
names=empl oyeeNames,
comment .
char=" $",bl
ank.li
nes.skip=TRUE)
pri
nt(
empl oy
ees)
pri
nt(
str(
employees))

#col.names:AChar act
erv ectorthatcontai
nst hecolumnnamesf ortheretur
neddat aframe
# as.i
s:Pleasespeci f
yt heBool eanv ect
orofsamel engthast henumberofcol umn.This
argumentwi l
lconvertthechar acterval
uest ofactorsbasedont heBooleanv alue.Forexample,
wehav et wocolumns( Fi
rstName,Occupat ion)andweusesas. i
s=c(TRUE,FALSE) .Thiswill
keeptheFirst
Nameaschar acter(notani mpli
citfactor)
,andOccupat i
onasFact ors
#skip:Pleasespecifythenumberofr owsy ouwantt oski pfr
om textf
ilebef orebeginni
ngthe
dataread.Forexample,ifyouwantt oskipt
op3r ecords,useskip=3

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
# EXCELFi l
es
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

#-
--
---
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
---
--
--
--
---
--
--
--
#Incasey oudon'thavereadxl(
youmaynotneedt ospeci
fyr
epos)
i
nstal
l.
packages('
readxl
',
repos="
http:
//cran.
rstudi
o.com/"
)

#Loadther eadxl
package
l
ibr
ary
(readxl)

#ToLocatet
heCurrentWorkingDir
ect
ory
get
wd(
)
set
wd(
"D:\
\RProgr
ammi ng\
\ Data"
)

#listt
hesheetsoft
heexcel
fil
e
excel_
sheet
s('
Sample-
Sal
es-
Data.
xlsx'
)

#Calli
nfofrom t
hesheet
susingread_excel
df<-r
ead_excel
('
Sample-
Sal
es-Dat
a.xl
sx',
sheet
='Sheet
1')
head(
df)
sum(df[
'
Value'
])
str
(df
)
summary
(df
)

#I
fyouhadmul
ti
plesheet
sthaty
ouwant
edt
oimpor
tint
oal
i
st,
youcoul
ddot
hiswi
thl
appl
y()
:

excel
_sheet
s('
Sampl
e-Super
stor
e.xl
s'
)

ent
ir
e_wor
kbook<-l
appl
y(excel_sheets(
"Sampl
e-Super
store.xl
s")
,
read_ excel,
path="Sample-
Superstore.
xls"
)

#Showentir
eli
st:
pr
int
(ent
ir
e_wor
kbook)

Quest
ion:
Howt
oreadf
ir
stsheet&secondsheetat
tri
but
e.

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
# XMLFi l
es
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

#Youcanr eadaxmlf i
lei
nRusi
ngt
he"
XML"package.Thi
spackagecanbei
nst
all
edusi
ng
fol
l
owingcommand.
i
nstal
l
.packages(
"XML"
)

#Loadthepackager
equi
redt
oreadXMLf
il
es.
l
ibr
ary
("XML"
)

#ToLocatet
heCurrentWorkingDir
ect
ory
get
wd(
)
set
wd(
"D:\
\RProgr
ammi ng\
\ Data"
)

#Alsoloadtheot
herr
equi
redpackage.
l
ibr
ary(
"methods"
)

#Giv
etheinputfil
enamet othef
unct
ion.
r
esul
t<-xmlParse(f
il
e="
input
.xml
")

#Pri
ntt heresul
t.
pr
int
(result
)

#Exractt
herootnodeformthexml
fil
e.
r
ootnode<-xmlRoot
(resul
t)

#Fi
ndnumberofnodesintheroot
.
r
oot
size<-xml
Size(
root
node)

#Pri
ntther esul
t.
pr
int
(root
size)

#Gett hefi
rstel
ementoft
hef
ir
stnode.
pr
int
(rootnode[[
1]]
[[
1]]
)

#Gett hefi
fthel
ementoft
hef
ir
stnode.
pr
int
(rootnode[
[1]
][
[5]
])
#Gett hesecondelementoft
het
hir
dnode.
pr
int
(rootnode[
[3]
][
[2]
])

#Conv er
ttheinputxmlfi
letoadat
aframe.
xmldataf
rame<-xml ToDataFr
ame(
"i
nput.
xml"
)
pr
int(
xmldataf
rame)

#-
--
----
--
--
--
---
--
--
--
--
--
----
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
# JSONFi l
es
#-
--
----
--
--
--
---
--
--
--
--
--
----
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
#-
--
----
--
--
--
---
--
--
--
--
--
----
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
#I
nt heRconsol e,youcani ssuethefoll
owingcommandt
oinst
all
ther
jsonpackage.
i
nstall
.packages("
rjson")
i
nstall
.packages("
jsonli
te")

#Loadthepackager
equi
redt
oreadJSONf
il
es.
l
ibr
ary
("j
sonli
te"
)
l
ibr
ary
("r
json"
)

#ToLocatet
heCurrentWorkingDir
ect
ory
get
wd(
)
set
wd(
"D:\
\RProgr
ammi ng\
\ Data"
)

#Giv
etheinputf
il
enamet othefunct
ion.
r
esul
t<-f
romJSON( "
art
ist
sen.
json"
)

#Thi sisbecauset hi
sJSONf i
leturnedoutt obesomet hi
ngcal l
ed‘NDJSON( Newlinedel i
mited
JSON) ’
,whi chmeanst herearemul ti
pleJSONv aluesinsidethisfi
leandeachoft heJSONv alues
i
sconsi deredasani ndependentobj ect.Inthisparti
cularcase,eachbusinessinf
ormationmakes
uponesi ngleJSONv aluet her
eforet herearemanyJSONv al
uesinsideofthisJSONf i
le.This
couldbeusedof t
eni ndatast r
eami ngsi t
uationswher eeachJSONdat acanbesepar atedfrom
otherpartsoft hef il
esot hateachJSONdat acanbepr ocessedwithoutwaiti
ngfort hewhol e
documentt ol oad.
#Any way ,‘
jsonli
te’actuall
yhasaf unctiont odealwi t
ht his‘NDJSON’f i
l
etypewi t
h‘stream_ i
n()

funct
ion,sowecanusei ti
nsteadlikebelow.

r
esul
t<-st
ream_
in(
fi
le(
"ar
ti
stsen.
json"
))

#Conv ertJSONf ilet


oadat afr
ame.
j
son_data_frame<-as. dat
a.f
rame(
resul
t)
str
(j
son_dat a_
frame)
head(j
son_ dat
a_ f
rame)
pri
nt(
json_data_frame)

r
esul
t<-st
ream_
in(
fi
le(
"mov
ies_
en.
json"
))

#Conv ertJSONf ilet


oadat afr
ame.
j
son_data_frame<-as. dat
a.f
rame(
resul
t)
str
(j
son_dat a_
frame)
head(j
son_ dat
a_ f
rame,1)
pri
nt(
json_data_frame)

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
# Database
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

TheRODBCpackagepr
ovi
desaccesstodat
abases(
incl
udi
ngMi
crosof
tAccessandMi
crosof
t
SQLSer
ver
)thr
oughanODBCint
erf
ace.

Thepr
imar
yfunct
ionsar
egi
venbel
ow.

Funct
ionDescr i
pti
on
odbcConnect(dsn,uid="",pwd="") Openaconnect iont
oanODBCdat abase
sql
Fetch(channel,sqtable)Readat ablefrom anODBCdat abaseintoadatafr
ame
sql
Query(channel,quer y
) Submi taquer yt oanODBCdat abaseandretur
ntheresul
ts
sql
Save(channel,my df,tablename=sqt able,append=FALSE)Wr i
teorupdate(append=Tr
ue)a
datafr
amet oat ablei ntheODBCdat abase
sql
Drop(channel,sqtable)Remov eat abl
ef rom t
heODBCdat abase
cl
ose(channel) Cl oset heconnecti
on

#RODBCExample
#i
mport2tabl
es(Cr
imeandPunishment)fr
om aDBMS
#i
ntoRdat
af r
ames(andcal
lthem cr
imedatandpundat
)

l
ibrary(
RODBC)
my conn<-odbcConnect("
mydsn",
uid="
Rob",pwd="
aardvar
k")
crimedat<-sqlFet
ch(myconn,"
Cri
me" )
pundat<-sqlQuery(
my conn,
"sel
ect*from Puni
shment"
)
close(myconn)

#Rhasabui l
t-
inpackagenamed" RMySQL"whi
chprovi
desnativ
econnect
ivi
tybetweenwi
th
My
Sqldat
abase.Youcaninst
all
thi
spackagei
ntheRenvi
ronmentusingt
hef
oll
owingcommand.

i
nst
all
.packages(
"RMy
SQL"
)

#Cr
eat
eaconnectionObj
ectt
oMy SQLdatabase.
#Wewil
lconnectt
othesampeldat
abasenamed" saki
l
a"t
hatcomeswi
thMy
Sql
inst
all
ati
on.

l
ibr
ary("
RMy SQL")
mysqlconnecti
on=dbConnect(MySQL(
),user
='r
etai
l
_dba'
,passwor
d='
cl
ouder
a',
dbname='ret
ail
_db'
,host
='192.
168.
153.
129')

#Listthet
abl
esavail
ableinthi
sdat
abase.
dbLi
stTabl
es(
mysqlconnecti
on)

#Queryt
he"act
or"t
abl
estogetallt
herows.
r
esul
t=dbSendQuer
y(mysql
connect
ion,
"sel
ect*f
rom or
der
_it
ems"
)

#St oret
heresulti
naRdat afr
ameobj
ect
.n=5i
susedt
ofet
chf
ir
st5r
ows.
data.f
rame=f et
ch(r
esul
t,n=5)
pri
nt(dat
a.f
ame)

result=dbSendQuery(
mysqlconnect
ion,
"select*f
rom pr
oduct
swher
epr
oduct
_id<20;
")
#Fet challt
herecor
ds(wit
hn=- 1)andstoreitasadatafr
ame.
dat a.
fr
ame=f et
ch(r
esult
,n=-1)
print(
data)
dbSendQuer
y(my
sql
connect
ion,
"updat
emt
car
ssetdi
sp=168.
5wher
ehp=110"
)

dbSendQuery
(mysql
connect
ion,
"i
nserti
ntomtcar
s(r
ow_names,mpg,cy
l,di
sp,hp,
drat
,wt,
qsec,v
s,am,
gear,
carb)
val
ues('
NewMazdaRX4Wag' ,
21,6,
168.5,
110,3.
9,2.
875,17.
02,0,
1,4,
4)"
)

dbSendQuer
y(my
sql
connect
ion,
'dr
opt
abl
eifexi
stsmt
car
s')

#=============================================================================
===
AdvancedProgr
ammi ngwithR
#=============================================================================
===

DoFast
erDat
aMani
pul
ati
onusi
ngThese7RPackages

I
ntr
oduct
ion

DataManipulationi
sani nevi
tabl
ephaseofpredi
cti
vemodel
ing.Arobustpredi
cti
vemodelcan’t
j
ustbebuiltusingmachinelearni
ngalgor
it
hms.But
,wit
hanapproachtounderst
andthebusiness
problem,the underl
yi
ng data,performi
ng r
equir
ed dat
a manipul
ati
ons and then ext
ract
ing
businessi
nsights.

Among t hese sever


alphases ofmodelbui ldi
ng,mostoft he t
ime i
s usual
l
y spentin
underst
andingunderl
yingdataandperf
ormingrequi
redmani
pul
ati
ons.Thi
swouldalsobethe
focusofthi
sarti
cle–packagestoper
for
mf ast
erdatamani
pul
ati
oninR.

Whati
sDat
aMani
pul
ati
on?

I
fy ouar
esti
l
lconf
usedwiththis‘
ter
m’,l
etmeexpl ai
ni tt
oy ou.DataManipul
ati
onisaloosely
usedter
m wi
th‘
DataExpl
orat
ion’
.Iti
nvol
ves‘manipul
ati
ng’datausingav
ail
ablesetofv
ari
ables.
Thisi
sdonet
oenhanceaccur
acyandpreci
sionassoci
atedwithdata.

Actually
,thedatacollect
ionpr ocesscanhav emanyloopholes.Therearevari
ousuncontr
ollabl
e
factorswhichleadtoinaccuracyindatasuchasment alsit
uati
onofrespondents,
per
sonalbiases,
diff
erence/errorinreadingsofmachi nesetc.Tomiti
gatetheseinaccur
acies,
datamanipulati
on
i
sdonet oincr
easethepossible( hi
ghest)accur
acyi
ndat a.

Att
imes,
thi
sst
agei
sal
soknownasdat
awr
angl
i
ngordat
acl
eani
ng.

Di
ff
erentWay
stoMani
pul
ate/Tr
eatDat
a:

Therei
snorightorwrongwayinmani pul
ati
ngdata,aslongasyouunder
standthedat
aandhav
e
takent
henecessaryacti
onsbyt heendoft heexer
cise.However
,herear
eaf ewbroadwaysi
n
whichpeopl
etryandapproachdatamanipul
ati
on.Herearethey
:

Usuall
y,beginnersonR f i
ndt hemsel vescomfortablemani pul
ati
ngdatausinginbui
ltbaseR
funct
ions.Thisisa goodf ir
stst ep,butisoft
enr epeti
tiveandt i
meconsuming. Hence,itisa
l
essef f
ici
entwayt osolv
ethepr oblem.
Useofpackagesf ordat
amani pulati
on.CRANhasmor et han7000packagesav ai
l
abletoday.In
simpl
ewor ds,thesepackagesar enothingbutacol l
ectionofpr e-wr
it
tencommonlyusedpi eces
ofcodes.Theyhel pyouperf
ormt herepeti
ti
vetasksf asts,reduceerr
orsi
ncodingandtakehel p
ofcodewr i
tt
enbyexper ts(acrosstheopensour ceeco- syst
em forR)tomakey ourcodemor e
effi
cient.Thisisusual
lythemostcommonwayofper f
ormi ngdatamanipul
ati
on.
UseofMLal gori
thmsfordat amanipulati
on.Youcanuset reebasedboosti
ngalgorit
hmst otake
careofmi ssi
ngdata&out li
ers.Whil
et hesearedefinit
elylesstimeconsuming,theseapproaches
typi
callyleaveyouwant i
ngforabet t
erunderstandingofdat aattheendofi
t.
Hence,mor e oft
en than not,use ofpackages i st he de-fact
o method to perform data
manipul ati
on.Inthisart
icl
e,Ihav eexplai
nedsever alpackageswhi chmake‘ R’li
feeasierdur
ing
thedat amani pul
ati
onstage.

Not
e:Thi
sar
ti
clei
sbestsui
tedf
orbegi
nner
sinRLanguage.Youcani
nst
all
apackagesusi
ng:

i
nst
all
.packages(
'packagename'
)

Li
stofPackages

Forbet
terunderst
andi
ng,I’
vealsodemonstratedt
heirusagebyunder
taki
ngcommonl
yused
oper
ati
ons.Bel
owistheli
stofpackagesdi
scussedi
nthi
sarti
cle:

dplyr
data.t
able
ggplot2
reshape2
readr
ti
dyr
l
ubr i
date

Not
e:Iunder standggpl ot
2isagr aphi
calpackage.But
,itgeneral
l
yhelpsinv i
suali
zingdata
(di
str
ibuti
ons,correl
ati
ons)andmakingmanipul
ati
onsaccor
dingl
y.Hence,I

veaddeditinthi
sli
st.
I
nallpackages,I’
vecoveredonl
ythemostcommonl yusedcommandsi ndatamani
pulati
on.

dpl
yrPackage

Thispackagesi screat
edandmai nt
ainedbyHadleyWickham.Thispackagehasev ery
thi
ng
(almost)toaccel
erat
eyourdat
amani pul
ati
onef
for
ts.I
tisknownbestfordataexpl
orat
ionand
transfor
mation.I
t’
schaini
ngsynt
axmakesi thi
ghl
yadaptivetouse.I
tincl
udes5maj ordata
mani pul
ati
oncommands:

fi
lt
er–I tfi
ltersthedatabasedonacondi t
ion
select–Itisusedt oselectcolumnsofinterestfr
om adataset
arr
ange–I tisusedt oarrangedatasetval
uesonascendi ngordescendi
ngorder
mut at
e–I tisusedt ocreatenewv ari
abl
esf rom exi
sti
ngvariabl
es
summar ise( wit
hgr oup_by)–I ti
susedt operform anal
ysisbycommonl yusedoperat
ionssuch
asmi n,max, meancountet c
Simplef ocusont hesecommandsanddogr eatindat aexplor
ati
on.Let’
sunderstandthese
commandsonebyone.Ihav eused2pre-i
nstall
edRdat asetsnamelymtcar
sandiri
s.

l
ibr
ary
(dplyr
)
dat
a("
mt car
s")
dat
a('
i
ris'
)

my
dat
a<-mt
car
s

#readdata
head(mydata)
#creatingal ocal dataframe.Local dataframear eeasi
ertoread
#data.frame( )isR'sf unct i
onf orcreatingr egulardatafr
ames.
#data_ f
rame( )isdpl yr'
sf unctionf orcreatinglocaldataframes.
#tbl_
df ()andas_ dat a_f r
ame( )ar edplyr'
sf unct i
onsforconverti
ngar egul
ardataframe(oral i
st)
i
ntoal ocal dat aframe.
#So,whati st hedi fferencebet weenr egularandl ocaldataframes?Ver yl
it
tl
e.Alocaldataframe
i
sjustar egul ardat af ramet hathasbeenwr appedwi t
ht hetbl_dfclassfornicerpri
nti
ng.(The
datai
sst i
l
lst oredi nar egulardat aframe" undert hehood".)
#Speci fi
cally,pr i
ntingal ocaldat af r
ameonl yshowst hef i
rst10r ows,andasmanycol umnsas
canfitony ourscr een.( Youcanseeanexampl eofthisbehavioratt hetopoft heRMar kdown
documentf rom myf i
rstdpl yrvideot ut
orial,whichprecedest hetutor
ialli
nkedabove).

my
newdata<-tbl
_df
(my dat
a)
my
iri
sdat
a<-tbl_
df(
ir
is)

#nowdatawil
lbei
ntabul
arst
ruct
ure
mynewdata
myir
isdat
a

#usefi
l
tert
ofil
terdatawi
thr
equi
redcondi
ti
on
fi
l
ter
(mynewdat
a, cy
l>4&gear>4)

f
il
ter
(my
newdat
a,cy
l>4)

f
il
ter
(my
iri
sdat
a,Speci
es%i
n%c(
'set
osa'
,'
vi
rgi
nica'
))

#usesel
ectt
opickcol
umnsbyname
sel
ect
(mynewdata,
cyl
,
mpg,
hp)

#her
eyoucanuse(-
)tohi
decol
umns
sel
ect
(mynewdat
a,-
cyl
,-
mpg)

#hi
dearangeofcol
umns
sel
ect
(mynewdat
a,-
c(cy
l,
mpg)
)

#sel
ectseri
esofcolumns
sel
ect(
my newdat
a,cyl
:gear
)

#chai
ningorpipel
i
ning-awaytoper
for
m mul
ti
pleoper
ati
ons
#i
nonel ine
mynewdat a%>%
select(
cyl
,wt,gear
)%>%
fi
lter(
wt>2)

#ar
rangecanbeusedt oreor
derr
ows
mynewdata%>%
sel
ect(
cyl,
wt,gear)
%>%
arr
ange(wt)

#ar
rangecanbeusedt oreor
derr
ows
mynewdata%>%
sel
ect(
cyl,
wt,gear)
%>%
arr
ange(wt)

my
newdat
a%>%
sel
ect(
cyl,wt,
gear)
%>%
arr
ange(desc(
wt))

#mut
ate-cr
eat
enewv
ari
abl
es

my
newdata%>%
sel
ect
(mpg,cyl
)%>%
mutat
e(newvar
iabl
e=mpg*
cyl
)

newv
ari
abl
e<-my
newdat
a%>%mut
ate(
newv
ari
abl
e=mpg*
cyl
)

#summar i
se-thi
sisusedt ofi
ndi
nsi
ghtsf
rom data
myir
isdat
a%>%
group_
by(Species)%>%
summar i
se(Average=mean(
Sepal
.Lengt
h,na.
rm =TRUE)
)

my
iri
sdata%>%
group_
by(Species)
%>%
summar i
se_each(f
uns(
mean,
n()
),Sepal
.Lengt
h,Sepal
.Wi
dth)

#y
oucanrenamethevar
iabl
esusi
ngr
enamecommand
mynewdat
a%>%rename(mil
es=mpg)

dat
a.t
abl
ePackage

Thi
spackageallowsyoutoperfor
mf ast
ermanipul
ati
oninadat
aset.Leav
ey ourtr
adit
ionalways
ofsubsetti
ngrowsandcol umnsanduset hi
spackage.Wi
thmini
mum coding,youcandomuch
more.Usingdata.
tabl
ehelpsinreduci
ngcomput i
ngtimeascomparedtodata.fr
ame.You’llbe
ast
onishedbythesimpli
cit
yofthi
spackage.

Adatatablehas3partsnamelyDT[i
,
j,
by]
.Youcanunder
standthi
sas,wecantellRtosubsett
he
r
owsusi ng‘i
’,
tocal
culate‘
j
’whichi
sgroupedby‘
by’
.Mostofthetimes,
‘by
’rel
atest
ocategori
cal
var
iabl
e.Inthecodebelow,I
’v
eused2datasets(
air
qual
i
tyandiri
s).

#loaddata
data("
air
qual
it
y")
my dat
a<-air
quali
ty
head(ai
rqual
i
ty,
6)

dat
a(i
ri
s)
myi
ris<-i
ri
s

#loadpackage
l
ibrar
y(dat
a.t
abl
e)

my
dat
a<-dat
a.t
abl
e(my
dat
a)
my
dat
a

my
iri
s<-dat
a.t
abl
e(my
iri
s)
my
iri
s

#subsetr
ows-sel
ect2ndt
o4t
hrow
my
dat
a[2:
4,]

#sel
ectcol
umnswit
hpart
icul
arv
alues
myir
is[
Speci
es=='
set
osa'
]

#sel
ectcolumnswi t
hmult
ipl
eval
ues.Thiswi
l
lgi
vey
oucol
umnswi
thSet
osa
#andvir
ginicaspeci
es
myir
is[
Species%in%c('
set
osa'
,'
vi
rgi
nica'
)]

#sel
ectcol
umns.Ret
urnsav
ect
or
mydata[
,
Temp]

my
dat
a[,
.
(Temp,
Mont
h)]

#r
etur
nssum ofsel
ect
edcolumn
mydat
a[,
sum(
Ozone,na.
rm =TRUE)
]

#r
etur
nssum andstandar
ddev i
ati
on
mydat
a[,
.
(sum(Ozone,na.
rm =TRUE),sd(
Ozone,
na.
rm =TRUE)
)]

#pr
intandplot
myir
is[,
{pr
int
(Sepal
.Lengt
h)
pl
ot(Sepal
.Width)
NULL}]

#gr
oupi
ngbyav ar
iabl
e
myi
ri
s[,
.
(sepal
sum =sum(
Sepal
.Lengt
h))
,by
=Speci
es]

#sel
ectacolumnforcomput
ati
on,
henceneedt
osett
hekeyoncol
umn
set
key(
myiri
s,Speci
es)

#sel
ectsall
t herowsassociatedwi
tht
hisdat
apoi
nt
myir
is[
'
setosa']
myir
is[
c('
setosa'
, '
vi
rgi
nica'
)]

r
eshape2Package

Ast henamesuggest s,thi


spackagei susef ulinreshapingdata.Weal lknow thedatacomei n
manyf orms.Hence,wear erequir
edt ot amei taccordi
ngt oourneed.Usually,theprocessof
reshapingdatainRi stedi
ousandwor risome.Rbasef uncti
onsconsistof‘Aggregati
on’opt i
on
usingwhichdat acanbereducedandr ear r
angedintosmal l
erfor
ms, butwit
hreductioninamount
ofi nf
ormation.Aggregati
onincludest apply,byandaggr egatebasef uncti
ons.Ther eshape
packageov ercometheseproblems.Herewet rytocombi nefeat
ureswhichhaveuni quevalues.It
has2f unct
ionsnamel ymeltandcast.

melt:Thi
sfunct
ionconvert
sdatafr
om wi
deformatt
olongf
ormat.I
t’
saform ofrest
ruct
uri
ng
wheremult
ipl
ecategor
icalcol
umnsare‘
melt
ed’i
ntouni
quer
ows.Let
’sunder
standitusi
ngthe
codebel
ow.

#cr
eateadat a
>ID<-c(1,2,
3,4,
5)
>Names<-c( '
Joseph'
,'
Mat r
in'
,'
Joseph'
,
'James',
'
Matri
n'
)
>DateofBirt
h<-c(1993,1992,1993,
1994,1992)
>Subject<-c('
Maths'
,'
Biol
ogy '
,'
Sci
ence'
,
'Psycol
ogy'
,
'Physi
cs'
)
>thi
sdata<-data.
fr
ame(
ID,
Names,
Dat
eof
Bir
th,
Subj
ect
)
>data.
tabl
e(t
hisdat
a)

#l
oadpackage
>i
nstal
l
.packages(
'r
eshape2'
)
>l
ibr
ary(
reshape2)

#melt
>mt<-melt
(thi
sdat
a,i
d=(
c('
I
D',
'
Names'
))
)
>mtcars

cast:Thisfuncti
onconvertsdatafrom l
ongformatt
owidef or
mat .I
tst
artswit
hmel t
eddataand
reshapesintolongformat .It
’sj
ustther ev
erseofmeltfuncti
on.Ithast wofuncti
onsnamely
,
dcastandacast.dcastreturnsadataframeasoutput
.acastretur
nsav ect
or/matr
ix/
arr
ayasthe
output.Let
’sunderst
anditusingthecodebelow.

#cast
>mcast<-dcast
(mt
,Dat
eof
Bir
th+Subj
ect~v
ari
abl
e)
>mcast

r
eadrPackage

Ast henamesuggests,‘
readr’
helpsinreadi
ngv ar
iousf
ormsofdataint
oR.Wi th10xfast
erspeed.
Here,charactersareneverconv er
tedtof act
ors(sonomor estr
ingAsFactors=FALSE).This
packagecanr epl
acethet r
adit
ionalread.
csv()andread.
tabl
e()baseR f uncti
ons.I
thelpsin
readingthefol
lowi
ngdata:

Del
imit
edfil
eswi t
hread_del
im()
,read_
csv(
),read_t
sv()
,andr
ead_csv2().
Fi
xedwidt
hf i
leswithread_f
wf()
,andread_
table()
.
Weblogfi
leswithread_log(
)
I
fthedataloadingtimei smorethan5seconds,t hi
sfunct
ionwillshowy ouaprogressbart
oo.
Youcansuppressthepr ogr
essbarbymarkingitasFALSE.Let’
slookatt hecodebel
ow:

>inst
all
.packages('
readr'
)
>li
brar
y (
readr)
>read_csv('
test
.csv
',
col_names=TRUE)
Youcanal sospecif
yt hedatat
ypeofev
erycol
umnl
oadedi
ndat
ausi
ngt
hecodebel
ow:

>read_
csv("
iris.csv"
,col_types=l ist(
Sepal.Length=col _doubl e(
) ,
Sepal.Width=col _double( )
,
Petal
.Lengt h=col_ double(),
Petal
.Wi dth=col _
doubl e(),
Species=col _factor(c(
"setosa","
versi
color"
,"v
irgi
nica")
)
))
However,i
fy ouchooset oomi tuni mportantcolumns,itwillt
akecar
eofi
taut
omat
ical
l
y.So,t
he
codeabovecanal sober e-wri
ttenas:

>read_csv("
ir
is.
csv"
,col
_types=list(
Species=col_fact
or(c(
"set
osa","
versi
col
or"
,"v
irgi
nica"
))
)
P.
S–r eadrhasmanyhel perfuncti
ons.So,nextwhenyouwr i
teacsvfi
l
e,usewr
it
e_csvi
nst
ead.
I
t’
sal otfast
erthanwrit
e.csv.
t
idy
rPackage

Thispackagecanmakey ourdatalook‘t
idy’
.Ithas4maj orf
uncti
onstoaccompl
i
shthistask.
Needlesstosay ,i
fyoufi
ndy our
sel
fstuckindataexplor
ati
onphase,y
oucanusethem any
time
(al
ong with dpl
y r
).Thi
sduo makesa f or
midableteam.Theyar eeasytolear
n,codeand
i
mpl ement.These4functi
onsare:

gather()–it‘gathers’multi
plecolumns.Then, i
tconver
tsthem i
ntokey:
val
uepai r
s.Thi
sfunct
ion
wil
lt r
ansform widef r
om ofdat atol ongform.Youcanusei tasinalt
ernat
iveto‘melt
’inr
eshape
package.
spread()– Itdoesr ev er
seofgat her.Ittakesakey :
valuepairandconv ert
sitintosepar
ate
columns.
separate(
)–I tsplit
sacol umni nt
omul ti
plecolumns.
unite(
)–I tdoesr everseofseparate.Itunit
esmul t
ipl
ecolumnsintosi
nglecolumn
Let’
sunder standitcloselyusi
ngt hecodebel ow:

#l
oadpackage
>l
ibr
ary
(ti
dyr)

#cr
eateadummydat aset
>names<-c( '
A'
,'
B',
'
C',
'
D','
E',
'
A',
'
B')
>weight<-c(
55,49,
76,71,65,
44,34)
>age<-c(21,
20,25,
29,33,32,
38)
>Class<-c('
Maths'
,'
Science'
,'
Social
'
,'
Phy
sics'
,
'Bi
ology
',
'Economi
cs'
,
'Account
s')

#creat
edataframe
>tdata<-dat
a.fr
ame(
names,
age,
wei
ght
,Cl
ass)
>tdata

#usinggatherfunct
ion
>long_t<-tdat
a%>%gat her
(Key
,Val
ue,
wei
ght
:Cl
ass)
>long_t

Separat
efuncti
oncomesbestinusewhenwear eprovi
dedadatetimevari
ableinthedataset
.
Since,t
hecolumncontai
nsmulti
plei
nfor
mati
on,henceitmakessensetospl
ititandusethose
valuesi
ndi
vi
duall
y.Usi
ngthecodebel
ow,Ihav
eseparat
edacolumnintodat
e,mont handyear
.

#createadat aset
>Humi dity<-c(37.79,42.
34, 52.16,44.57,43.
83,44.
59)
>Rai n<-c(0.
971360441, 1.10969716, 1.064475853,
0.953183435,0.
98878849,0.
939676146)
>Ti me<-c( "
27/ 01/201515: 44",
"23/02/201523:24","
31/03/201519:15"
,"20/01/
201520:52"
,
"23/02/201507: 46","
31/01/201501: 55")

#bui
ldadataframe
>d_set<-dat
a.fr
ame(
Humi
dit
y,Rai
n,Ti
me)

#usi
ngseparat
efunct
ionwecansepar
atedate,
month,y
ear
>separ
ate_
d<-d_set%>%separ
ate(
Time,c(
'Dat
e',
'Mont
h',
'
Year
')
)
>separ
ate_
d

#usi
nguni
tefuncti
on-rev
erseofsepar
ate
>uni
te_
d<-separate_
d%>%unite(
Time,c(
Dat
e,Mont
h,Year
),sep="
/")
>uni
te_
d

#usi
ngspr
eadf
unct
ion-r
ever
seofgat
her
>wi
de_
t<-l
ong_
t%>%spr
ead(
Key
,Val
ue)
>wi
de_
t

Lubr
idat
ePackage

Lubridat
epackagereducesthepai
nofworki
ngofdat
ati
mev ar
iabl
einR.Thei
nbuil
tfunct
ionof
thispackageoffer
sani cewayt omakeeasyparsi
ngindatesandtimes.Thispackagesis
frequent
lyusedwi t
hdat acompri
singofti
mel
ydata.Her
eIhav ecover
edthreebasictasks
accompl i
shedusi
ngLubridat
e.

Thisincl
udesupdatefuncti
on,durati
onf uncti
onanddateext r
acti
on.Asabegi nner,knowing
these3functi
onswouldgivey ougoodenoughexper t
isetodealwithti
mev ar
iables.Though,R
hasinbui
ltf
uncti
onsforhandl
ingdates,butthi
sismuchfast
er.Let’
sunderst
anditusingthecode
below:

>i
nst
all
.
packages('
l
ubr
idat
e')
>l
i
brar
y(l
ubr
idate)

#cur
rentdat
eandt
ime
>now()

#assi
gni
ngcurr
entdat
eandt
imet
ovar
iabl
en_
time
>n_ti
me<-now()

#usi
ngupdat
efuncti
on
>n_updat
e<-updat
e(n_
time,
year=2013,
mont
h=10)
>n_updat
e

#adddays,months,year,
seconds
>d_ti
me<-now( )
>d_ti
me+dday s(1)
[
1]"2015-
12-1213:24:54IST"
>d_ti
me+dweeks( 2)
[
1]"2015-
12-1213:24:54IST"

>d_
time+dyear
s(3)
[
1]"
2018-
12-
1013:24:
54I
ST"

>d_
time+dhour
s(2)
[
1]"
2015-
12-
1115:24:
54I
ST"

>d_
time+dminut
es(50)
[
1]"
2015-
12-
1114:14:54I
ST"

>d_
time+dseconds(
60)
[
1]"
2015-
12-
1113:25:54I
ST"

#extr
actdate,
ti
me
>n_ti
me$hour<-hour (now( ))
>n_ti
me$mi nute<-minut e(now()
)
>n_ti
me$second<-second( now(
))
>n_ti
me$mont h<-mont h(now())
>n_ti
me$y ear<-year(
now( )
)
#checktheextracteddatesi nsepar
atecol
umns
> new_ data <- dat a.frame(n_t
ime$hour
, n_
time$mi
nut
e, n_
time$second, n_
time$mont
h,
n_
time$year
)
>new_data

Note:Thebestuseofthesepackagesi
snotini
solat
ionbuti
nconj
unct
ion.Youcoul
deasi
lyuse
t
hispackagewithdplyrwher
ey oucaneasi
lysel
ectadatavari
abl
eandextractt
heusef
uldata
f
rom itusi
ngthechai
ncommand.

EndNot
es

Thesepackageswouldnotonlyenhanceyourdatamani pul
ati
onexper
ience,butal
sogi
vey ou
reasonstoexpl
oreRindept
h.Nowwehav eseen,t
hesepackagesmakecodi nginReasi
er.You
nolongerneedt
owrit
elongcodes.I
nsteadwri
teshortcodesanddomore.

Everypackagehasmul titaski
ngabili
ti
es.Hence,Iwoul dsuggesty outogetholdofimportant
functi
onwhichcanbeusedf r
equentl
y.And,onceyougetf amil
iarwit
hthem,youcandigdeeper.I
didthi
smi st
akeinit
ial
ly.Itr
iedatexpl
oringall
thefeatur
esi nggplot
2andendedupi naconfusi
on.
I
’dsuggesty outopr acticethesecodesasy ouread.Thiswoul dhelpyoubuil
dconfidenceon
usingthesepackages.

I
nthisar
ti
cle,I’
veexpl
ainedtheuseof7Rpackageswhi chcanmakedataexpl
orat
ioneasi
erand
f
aster
.Rknownf orit
sawesomest at
ist
icalf
unct
ions,wit
hnewlyupdatedpackagesmakesa
f
avori
tet
ool ofdat
ascienti
ststoo.

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

Abr
iefi
ntr
oduct
iont
o“appl
y”i
nR

So,whataret
hesewondrousappl
yfunct
ionsandhowdotheywork?It
hinkthebestwaytofigur
e
outanyt
hingi
nRistolear
nbyexperi
mentati
on,usi
ngembar
rassi
ngl
ytr
ivial
dataandfunct
ions.

I
fyoufir
eupyourRconsole,type“
??appl
y”andscr
olldownt
othef
unct
ionsi
nthebasepackage,
y
ou’l
lseesomet
hingl
ikethis:

base:
:
apply ApplyFuncti
onsOv erArrayMar gi
ns
base:
:
by ApplyaFunct
iontoaDat aFrameSpli
tbyFactor
s
base:
:
eapply ApplyaFunctionOv erValuesinanEnvi
ronment
base:
:
lappl
y ApplyaFuncti
onov eraListorVector
base:
:
mapply ApplyaFunctiontoMultipleLi
storVect
orArgument
s
base:
:
rappl
y Recursi
vel
yAppl yaFunctiontoaList
base:
:
tappl
y ApplyaFuncti
onOv eraRaggedAr r
ay

Let
’sexami
neeachoft
hose.

1.appl
y
Descri
pti
on:“Ret
urnsavect
ororar
rayorl
i
stofv
aluesobt
ainedbyappl
yi
ngaf
unct
iont
omar
gins
ofanarrayormatri
x.”

OK–weknowaboutv ect
ors/ar
ray
sandfunct
ions,butwhatar
ethese“ mar
gins”?Si
mple:ei
ther
t
herows(1)
,thecolumns( 2)orbot
h(1:2)
.By“ both”
,wemean“ applyt
hef unct
iontoeach
i
ndi
vi
dual
val
ue.”Anexample:

#creat
eamat ri
xof10rowsx2col
umns
m <-matr
ix(
c(1:
10,11:
20)
,nr
ow=10,ncol
=2)
#meanoft herows
appl
y(m,1,mean)
[1] 6 7 8 9101112131415
#meanoft hecolumns
apply
( m,2,mean)
[1] 5.515.5
#divideall
v al
uesby2
apply
( m,1:
2, f
uncti
on(
x)x/
2)

[,
1][
,
2]
[
1,] 0.
5 5.5
[
2,] 1.
0 6.0
[
3,] 1.
5 6.5
[
4,] 2.
0 7.0
[
5,] 2.
5 7.5
[
6,] 3.
0 8.0
[
7,] 3.
5 8.5
[
8,] 4.
0 9.0
[
9,] 4.
5 9.5
[
10,] 5.
010.0

Thatl
astexampl
ewasr
athert
ri
vi
al;
youcoul
djustaseasi
l
ydo“
m[,
1:2]
/2”–buty
ougett
hei
dea.

2.by

Updated27/2/14:notethattheor
igi
nalexampleinthi
ssecti
onnol ongerworks;usecol
Means
nowinsteadofmean.
Descr
ipti
on:“Funct
ion‘by’
isanobj
ect-
ori
entedwrapperf
or‘
tappl
y’appli
edtodataframes.

Thebyf uncti
oni sali
tt
lemorecomplexthanthat
.Readalit
tl
ef ur
therandthedocument
ati
on
tel
lsyouthat“ adat
af r
ameisspli
tbyrow int
odataframessubsett
edbyt heval
uesofoneor
mor efact
ors,andfunct
ion‘
FUN’isappl
iedtoeachsubsetintur
n.”So,weuset hi
sonewhere
fact
orsareinvolv
ed.

Toil
lustr
ate,wecanl
oadupt
hecl
assi
cR dat
aset“
ir
is”
,whi
chcont
ainsabunchoff
lower
measurements:

att
ach(i
ri
s)
head(i
ri
s)
Sepal
.Lengt
hSepal
.
WidthPet
al.
Lengt
hPetal
.Wi
dthSpeci
es
1 5.
1 3.5 1.
4 0.
2 setosa
2 4.
9 3.0 1.
4 0.
2 setosa
3 4.
7 3.2 1.
3 0.
2 setosa
4 4.
6 3.1 1.
5 0.
2 setosa
5 5.
0 3.6 1.
4 0.
2 setosa
6 5.
4 3.9 1.
7 0.
4 setosa

#gett hemeanoft hefir


st4v ari
abl
es,byspecies
by(i
ri
s[,1:4]
,Species,col
Means)
Species:setosa
Sepal.Length Sepal.WidthPetal.
Length Petal.
Width
5.006 3.428 1.462 0.
246
--
--
--
--
----
--
--
--
---
--
--
--
--
--
--
---
--
---
--
--
--
--
--
--
--
--
--
-
Species:versi
color
Sepal.Length Sepal.WidthPetal.
Length Petal.
Width
5.936 2.770 4.260 1.
326
--
--
--
--
----
--
--
--
---
--
--
--
--
--
--
---
--
---
--
--
--
--
--
--
--
--
--
-
Speci
es:vir
gini
ca
Sepal
.Length Sepal
.Widt
hPet
al.
Lengt
h Pet
al.
Width
6.588 2.
974 5.
552 2.
026

Essent
ial
l
y,byprov
idesawayt ospli
tyourdat
abyfact
orsanddocal
cul
ati
onsoneachsubset
.It
ret
urnsanobj
ectofclass“
by”andtherear
emany,morecomplexway
stouseit
.

3.eappl
y
Descri
pti
on:“eappl
yappl
i
esFUN t
othenamedv
aluesf
rom anenv
ironmentandr
etur
nst
he
resul
tsasali
st.”

Thisonei sal i
tt
letr
icki
er,si
nceyouneedt oknow somet hi
ngaboutenvi
ronment
si nR.An
envir
onment,ast henamesuggest s,i
sa self-
contai
ned objectwi
thit
sown v ar
iabl
esand
functi
ons.Toconti
nueusi
ngourver
ysimpleexample:

#anewenv i
ronment
e<-new.env()
#t woenvi
ronmentv ari
abl
es,
aandb
e$a<-1:10
e$b<-11:20
#meanoft hev ar
iables
eapply
(e,mean)
$b
[1]15.
5

$a
[
1]5.
5

Idon’
toftencreat
emyownenv ir
onment
s,butthey
’recommonl
yusedbyRpackagessuchas
Bi
oconductorsoit
’sgoodt
oknowhowtohandl
ethem.

4.l
apply
Descr
ipt
ion:“
lapplyret
urnsali
stoft
hesamelengt
hasX,eachel
ementofwhi
chi
sther
esul
tof
appl
yi
ngFUNt ot hecorr
espondi
ngel
ementofX.

That’
s a ni
ce,cl
eardescr
ipti
on whi
ch makes l
appl
yone oft
he easi
erappl
yfunct
ions t
o
underst
and.Asi
mpleexample:

#createali
stwith2el
ement
s
l<-l
ist
(a=1:10,b=11:
20)
#themeanoft heval
uesi
neachel
ement
l
apply(
l,mean)
$a
[
1]5.5

$b
[
1]15.
5

#thesum oft
hev
aluesi
neachel
ement
l
apply
(l,
sum)
$a
[
1]55

$b
[
1]155
Thel applydocument
ati
ont
ell
sust
o consul
tfur
therdocument
ati
onf
orsappl
y,v
appl
yand
repl
i
cat e.Let
’sdot
hat
.

4.
1sapply
Descri
pti
on:“sappl
yisauser
-f
ri
endl
yver
sionofl
appl
ybydef
aul
tret
urni
ngav
ect
orormat
ri
xif
appropr
iat
e.”

Thatsimplymeanst hatifl
applywoul
dhav eret
urnedali
stwi
thel
ement
s$aand$b,sappl
ywil
l
ret
urneit
herav ector
,withelement
s[[
‘a’
]]and[[
‘b’
]]
,oramatr
ixwi
thcol
umnnames“a”and“b”
.
Retur
ningtoourprevioussi
mpleexample:

#cr eateal i
stwith2elements
l<-list
(a=1: 10,b=11:20)
#meanofv aluesusingsappl
y
l
.mean<-sappl y(l
,mean)
#whatt ypeofobjectwasr et
urned?
class(l
.mean)
[1]"numer i
c"
#i t
'sanumer i
cvector,sowecangetel
ement"
a"l
i
ket
his
l
.mean[ ['
a']
]
[1]5.5

4.2vappl
y
Descri
pti
on:“
vapplyi
ssimil
artosappl
y,buthasapr
e-speci
fi
edt
ypeofr
etur
nval
ue,soi
tcanbe
safer(
andsometimesfast
er)t
ouse.”

Athi
rdargumentissuppl
i
edtov appl
y,whichyoucant
hinkofasakindoft
emplat
efort
heout
put
.
Thedocumentat
ionusesthef
ivenum functi
onasanexample,
solet
’sgowit
hthat
:

l<-list(
a=1: 10,b=11: 20)
#f ivenum ofv al
uesusingvappl
y
l
.fivenum <-v appl
y(l
,fi
venum,c(
Min.
=0,
"1stQu.
"=0,
Medi
an=0,
"3r
dQu.
"=0,
Max.
=0)
)
class(l.
fi
v enum)
[1]"mat r
ix"
#l et'
sseei t
l
.fivenum
a b
Mi n. 1.011.0
1stQu. 3. 013.0
Medi an 5. 515.
5
3rdQu. 8. 018.0
Max. 10. 020.0

So,v
applyr
etur
nedamat
ri
x,wherethecolumnnamescor
respondt
otheor
igi
nall
i
stel
ement
s
andther
ownamestot
heout
puttemplat
e.Nice.

4.
3repl
icat
e
Descri
pti
on:“r
epli
cat
ei sawr apperforthecommonuseofsapplyf orr
epeat
edev
aluat
ionofan
expr
ession(whichwi
llusual
l
yi nvol
verandom numbergener
ati
on)
.”

Therepli
catefunct
ionisveryusef
ul.Gi
veitt
womandatoryar
gument
s: thenumberofrepl
icat
ions
andthefuncti
ont orepl
icat
e;athi
rdopti
onalar
gument,si
mpli
fy=T,t
riestosimpli
fyther
esultto
av ectorormat ri
x.An ex ample– let’
s si
mulat
e 10 normaldistri
but
ions,each with 10
observ
ations:
r
eplicat
e(10,r
norm(10) )
[,
1] [,
2] [,
3] [,
4] [
,5] [
,6]
[
1,] 0.67947001-1.94649409 0. 28144696 0. 5872913 2. 22715085-0.275918282
[
2,] 1.17298643-0.01529898-1.47314092- 1.3274354-0.04105249 0. 528666264
[
3,] 0.77272662-2.36122644 0. 06397576 1. 5870779-0.33926083 1. 121164338
[
4,]-0.
42702542-0.90613885 0. 83645668- 0.5462608-0.87458396-0.723858258
[
5,]-0.
73892937-0.57486661-0.04418200- 0.1120936 0. 08253614 1. 319095242
[
6,] 2.93827883-0.33363446 0. 55405024- 0.4942736 0. 66407615-0.153623614
[
7,] 1.30037496-0.26207115 0. 49818215 1. 0774543-0.28206908 0. 825488436
[
8,]-0.
04153545-0.23621632-1.01192741 0. 4364413-2.28991601-0.002867193
[
9,] 0.01262547 0. 40247248 0. 65816829 0. 9541927-1.63770154 0. 328180660
[
10,] 0.96525278-0.37850821-0.85869035- 0.6055622 1. 13756753-0.371977151
[,
7] [,
8] [
,9] [
,10]
[
1,] 0.03928297 0. 34990909-0.3159794 1. 08871657
[
2,]-0.
79258805-0.30329668-1.0902070 0. 73356542
[
3,] 0.10673459-0.02849216 0. 8094840 0. 06446245
[
4,]-0.
84584079-0.57308461-1.3570979- 0.89801330
[
5,]-1.
50226560-2.35751419 1. 2104163 0. 74650696
[
6,]-0.
32790991 0. 80144695-0.0071844 0. 05742356
[
7,] 1.36719970 2. 34148354 0. 9148911 0. 20451421
[
8,]-0.
51112579-0.53658159 1. 5194130- 0.94250069
[
9,] 0.52017814-1.22252527 0. 4519702 0. 08779704
[
10,] 1.35908918 1. 09024342 0. 5912627- 0.20709053

5.mapply
Descri
pti
on:“
mappl
yisamul t
ivar
iat
eversionofsappl
y.mappl
yappli
esFUNt
othef
ir
stel
ement
s
ofeach(…)ar
gument
,thesecondelements,thet
hir
delement
s,andsoon.

Themappl
ydocument
ati
oni
sful
lofqui
tecompl
exexampl
es,
buther
e’sasi
mpl
e,si
l
lyone:

l
1<-li
st(a=c(
1:10),b=c(11:
20))
l
2<-li
st(c=c(
21:30),d=c(31:
40))
#sum thecor
respondingel
ementsofl
1andl
2
mappl
y(sum,l
1$a,l1$b,l
2$c,l
2$d)
[
1] 64 68 72 76 80 84 88 92 96100

Her
e,wesum l 1$a[1]+l1$b[
1]+l2$c[
1]+l2$d[1](1+11+21+31)toget64,thefi
rstel
ementof
t
hereturnedli
st.All
thewayt hr
oughtol1$a[10]+l1$b[
10]+l
2$c[
10]+l
2$d[10](10+20+30+40)
=100,thelastelement.

6.r
apply
Descri
pti
on:
“rappl
yisar
ecur
siv
ever
sionofl
appl
y.

It
hink“r
ecursiv
e”i
sal i
tt
lemi
sleadi
ng.Whatrappl
ydoesisappl
yfuncti
onst
oli
stsi
ndi
ff
erent
ways,
dependingonthear
gumentssuppl
i
ed.Besti
ll
ust
rat
edbyexamples:

#l et'
sstartwithourusualsi
mpleli
stexample
l<-list
(a=1: 10,b=11:20)
#l og2ofeachv aluei
ntheli
st
rapply(l,
log2)
a1 a2 a3 a4 a5 a6 a7 a8
0.0000001. 0000001.5849632.0000002.3219282.5849632.
8073553.000000
a9 a10 b1 b2 b3 b4 b5 b6
3.1699253. 3219283.4594323.5849633.7004403.8073553.
9068914.000000
b7 b8 b9 b10
4.0874634. 1699254. 2479284.321928
#l og2ofeachv al
ueineachl i
st
rapply(l,
log2,how=" l
ist"
)
$a
[1]0.0000001. 0000001.5849632.0000002.
3219282.
5849632.
8073553.
000000
[9]3.1699253. 321928

$b
[
1]3.
4594323.
5849633.
7004403.
8073553.
9068914.
0000004.
0874634.
169925
[
9]4.
2479284.
321928

#whatifthef
unct
ioni
sthemean?
r
apply
(l,
mean)
a b
5.
515.5

rappl
y(l
,mean,
how="
li
st"
)
$a
[
1]5.5

$b
[
1]15.
5

So,theoutputofrapplydependsonboththefunct
ionandthehowargument.Whenhow=“li
st”
(or“repl
ace”
),theorigi
nalli
ststr
uct
ureispreserv
ed.Other
wise,t
hedefaul
tistounl
ist
,whi
ch
resul
tsinavector
.

Youcanalsopassa“ cl
asses=”ar
gumenttor
appl
y.Forexampl
e,i
namixedl
i
stofnumeri
cand
charact
ervar
iabl
es,youcouldspeci
fythatt
hefunct
ionactonlyont
henumeri
cv al
ueswi
th
“cl
asses=numeri
c”.

7.t
apply
Descr
ipt
ion:“
Appl
yafunct
iont
oeachcel
lofaraggedarr
ay,t
hati
stoeach(
non-
empt
y)gr
oupof
val
uesgivenbyauni
quecombi
nat
ionoft
helev
elsofcer
tai
nfact
ors.

Woaht her
e.Thatsoundscompl i
cated.Don’tpani
cthough,
itbecomescl earerwhentherequi
red
argumentsaredescri
bed.Usageis“tappl
y(X,I
NDEX,FUN=NULL, …,simpli
fy=TRUE) ”
,whereXis
“anatomicobject
,ty
pical
lyavect
or”andINDEXi s“ali
stoff
actors,eachofsamel engt
hasX” .

So,t
ogobacktothefamousir
isdata,“
Speci
es”mightbeafact
orand“
ir
is$Pet
al.
Widt
h”woul
d
gi
veusav
ectorofval
ues.Wecoul
dthenrunsomethi
ngli
ke:

attach(i
ri
s)
#meanpet all
engthbyspecies
tapply(i
ri
s$Pet
al.
Length,Speci
es,mean)
set
osaversi
color vir
gini
ca
1.
462 4.260 5.552

#Built
-i
nRFeat ures-DataSt
ructur
es
#Rcont ainsquit
eaf ewusefulbui
lt-
inf
unct
ionst
owor
kwi
thdat
ast
ruct
ures.Her
ear
esomeof
thekeyfuncti
onst oknow:
seq()
:Createsequences
sort
():
Sortav ect
or
rev(
):Reverseelementsinobject
str
():Showt hestr
uctur
eofanobj ect
append():Mergeobjectstogether(worksonv
ect
orsandl
i
sts)

#seq(start,
end,st
epsize)
seq(0,
100, by=3)
num <-c(1,4,
6,
7,2,13,
2)
sort
(num )
sort
(num, decreasing=TRUE)

num2<-c(1,
2,
3,
4,
5)
rev(
num2)
str
(num)

append(num,
num2)
sort
(append(
num,
num2)
)

a<-c(
1,2,
3)
i
s.v
ector(
a)

#DataTy pes
#i
s.*
():Checkthecl
assofanRobj
ect
#as.
*()
:ConvertRobject
s

v<-c(1,
2,3)
i
s.v
ector(v)
i
s.l
i
st(v)
as.
li
st(v
)
as.
mat r
ix(v
)

#--
--
--
--
----
---
---
--
--
---
--
---
--
--
--
--
--
--
--
---
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
#Buil
t-
inRFeat ures-Mat h
#We'vet alkedabi taboutsomeoft hebuilt
-i
nmat hfuncti
onsandf
eat
uresi
nR,
butl
et'
shav
eone
morelookataf ewoft hem:
#abs():comput est heabsolutevalue.
#sum( )
: retur
nst hesum ofallthev al
uespresentintheinput
.
#mean( ):comput est heari
thmeticmean.
#round(): r
oundsv alues(addi
tionalargumentstonearest)

vec<-c( -
1,0,
1,
2,3,
4,
5)
abs(-
2)
abs(vec)
sum( vec)
mean( vec)
round(23.1231)
round(23.1231234,
2)

#Numer icFuncti
ons
#Funct i
on Descr i
pti
on
#abs(x)absol utev al
ue
#sqrt(x)squarer oot
#ceili
ng( x) ceili
ng(3.
475)is4
#fl
oor (x)fl
oor(
3.475)is3
#trunc(x) t runc(5.
99)is5
#round( x,di
git
s=n) r ound(
3.475,
digi
ts=2)i
s3.
48
#si
gnif
(x,di
git
s=n) si gni
f(
3.475,di
git
s=2)is3.5
#cos(
x),si
n(x)
,tan(x)al soacos(x)
,cosh(x)
,acosh(x)
,et
c.
#l
og(x) natur
allogari
thm
#l
og10(x) commonl ogari
thm
#exp(
x)e^ x

#--
--
---
--
--
--
--
--
--
--
--
---
--
---
--
--
--
--
---
---
--
--
--
--
--
--
--
--
---
--
--
--
---
--
--
--
-
#Fornow we' l
llearnaboutt wousef ulfunctionsforregul
arexpressi
onsandpat
ter
nsear
chi
ng
(we'l
lgodeeperintothistopicingenerallat
eron):
#grepl(
),whi
chr et
urnsal ogicali
ndicat
ingifthepatter
nwasf ound
#grep(),
whichreturnsav ectorofindexlocati
onsofmat chi
ngpatter
ninst
ances

text<-"Hithere,doyouknowwhoy
ouar
evot
ingf
or?
"
grepl(
'voti
ng',
text)
grepl(
'Hi'
,t
ext)
grepl(
'Sammy ',t
ext)
v<-c('a',
'
b',
'
c',
'
d')
grep('
a',
v)
grep('
c',
v)

#--
--
--
--
--
--
---
--
--
--
---
--
--
--
--
--
--
--
---
--
--
---
--
--
--
--
---
--
---
--
--
--
--
--
--
--
--
#Timestamps
#Rgivesusav ari
etyoftoolsforwor ki
ngwi thti
mestampinfor
mat i
on.Let
'sstartoffbyexplori
ng
theDateobject:
#Dates
#Youcanuset heas.Date(
)f uncti
ont oconv er
tacharacterstr
ingtoaDat eobj ect,whichwill
all
owittocontainmor eti
mei nformat i
on.Thestri
ngwill
needtobei nastandardtimef or
mat .We
canaskf ort
oday '
sdatebyaski ngthesy st
em (Sys.
)fort
heDate:

Sy
s.Dat
e()

#Setasavari
able
t
oday<-Sy
s.Date(
)
t
oday

#Youcanalsoconv ertcharacterstr
ingsi
nRtoaDateobjectusi
ngas.Dat
e()
.#You'
l
lneedt
o
makesureit
sint hecor r
ectformat,oruse%sy
mbol
sthatcor
rel
atewit
hyourgi
venf
ormat:
#Code Value
#%dDayofthemont h(decimalnumber )
#%m Month(decimal number )
#%bMonth(abbreviated)
#%BMonth(full
name)
#%yYear(2digi
t)
#%YYear(4digi
t)

#YYYY-MM- DD
as.
Date(
'1990-11-03'
)
#UsingFormat
as.
Date(
"Nov-03-90",
format="%b-%d-%y")
#UsingFormat
as.
Date(
"November -
03-1990",
format="
%B-%d-%Y"
)
as.
POSIXct("
11:02:03",
format="%H:%M:%S")
as.
POSIXct("
Nov ember-03-199011:02:
03",f
ormat
="%B-
%d-
%Y%H:
%M:
%S"
)

st
rpt
ime(
"11:
02:
03"
,f
ormat
="%H:
%M:
%S"
)
#=============================================================================
===
Dat
aMani pul
ati
oninR
#=============================================================================
===

#-
--
--
--
--
--
---
--
--
---
--
--
---
--
--
---
--
---
--
--
--
--
---
--
--
--
--
--
---
--
---
--
---
--
--
--
#l
apply(
)
#l
apply(
)willapplyafunct i
onov erali
storv ector:
#l
apply(
X,FUN, ..
.)
#whereXisy ourli
st/vectorandFUNi sy ourfunction.Formor
einf
oyoucanuse:

hel
p(l
appl
y)

#samplej
ust1r
andom numberbet
ween1and10
sampl
e(x=1:
10,
1)

#vect
or
v<-c(
1,2,
3,
4,
5)

#ourcustom funct i
on
addr
and<-f unction(x)
{
#Getar andom number
ran<-sampl e(x=1:10,
1)
#returnxpl ust herandom number
ret
urn(x+ran)
}

#lappl
y()
l
apply(
v,addr
and)

pr
int
(v)

#Anonfuncwithl
apply
()
l
appl
y(v
,funct
ion(
a){
a+sampl
e(x=1:
10,
1)}
)

#addstwot oeveryelement
l
appl
y(v
,funct
ion(x)
{x+2})

add_
choice<-funct
ion(num,
choi
ce)
{
ret
urn(num+choice)
}

add_
choi
ce(
2,3)

#Uhoh!Forgottoaddot
herar
gument
s!
l
appl
y(v
,add_choi
ce)

#Nice!
l
apply(
v,add_
choi
ce,
choi
ce=10)

#sappl
y()v
s.l
apply(
)
#Noti
cethatl
applyret
urnedalist
,wecanusesappl
y,whi
chsi
mpl
i
fiest
hepr
ocessbyr
etur
ninga
vect
orormatr
ix.Forexample:
hel
p(sappl
y)

#Nice!Av ect
orret
urned
sappl
y(v
,add_choi
ce,
choice=10)

#let
'sproveitt
oour selv
es
l
app<-lapply
(v,
add_ choi
ce,choi
ce=10)
sapp<-sapply(
v,add_choice,
choi
ce=10)

cl
ass(
lapp)#ali
st
cl
ass(
sapp)#vectorofnumer
ics

#Checksf orevennumbers
even<-functi
on(x){
ret
urn(
x[(x%%2==0) ]
)
}

nums<-c(
1,2,
3,
4,
5)

sappl
y(nums,
even)

l
appl
y(nums,
even)

#=============================================================================
===
Dat
aVi sual
i
zati
onwi t
hggplot2
#=============================================================================
===

#---
--
----
--
---
----
---
--
---
----
--
----
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
--
---
--
--
--
# Li
neGr aphs
#---
--
----
--
---
----
---
--
---
----
--
----
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
--
---
--
--
--
Thebasi csy ntaxt ocreat eal i
nechartinRi s:
plot(
v ,
type,col ,
xl
ab,y l
ab)
Foll
owi ngi st hedescr iptionofthepar ametersused:
vi sav ect orcont ainingt henumer i
cv al
ues.
typet akest hev alue" p"todrawonl ythepoints,"i
"todr
awonl
ythel
i
nesand"
o"
todr awbot hpoi ntsandl i
nes.
xlabi sthel abel forxaxi s.
y l
abi sthel abel foryaxi s.
mai nistheTi t
leoft hechar t.
col isusedt ogi vecol orstobot hthepointsandl i
nes.

#Createt hedat aforthechar


t.
v<-c(7,
12, 28,3,
41)
#Givethechar tfi
l
eaname.
png(
fil
e=" l
ine_chart
.j
pg")
#Plotthel inechart.
pl
ot(v
,type=" o")
#Savet hef il
e.
dev.
off(
)

#Plotthelinechart
.
pl
ot(v
,type="o"
,col
="r
ed"
,xl
ab="
Mont
h",
yl
ab="
Rai
nfal
l
",mai
n="
Rai
nfal
lchar
t"
)
#Savethef
il
e.
dev
.of
f()

#Cr eatet hedat af orthechar t.


v<-c(7,12, 28,3,
41)
t<-c(14, 7,
6,19,3)
#Giv ethechar tfi
leaname.
png(fi
le=" l
ine_char t_2_
lines.
jpg")
#Plott hebarchar t
.
plot
(v,t
y pe="o" ,
col="red"
,xlab="Month"
,y
lab="
Rai
nfal
l
",mai
n="
Rai
nfal
lchar
t"
)
l
ines(t,
t ype="o" ,
col ="bl
ue")
#Sav et hef il
e.
dev.off
()

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
# BarChart
s
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

Thebasi csynt axtocreat


eabar-chartinRis:
bar
plot(H,xl
ab,y l
ab,main,
names.arg,
col)
Fol
lowingi sthedescr i
pti
onofthepar amet
ersused:
Hi sav ectorormat ri
xcontai
ningnumer i
cvaluesusedi
nbarchar
t.
xl
abi sthel abel f
orxaxis.
yl
abi sthel abel f
oryaxis.
mai nisthet i
t l
eofthebarchart.
names. argisav ect
orofnamesappear i
ngundereachbar.
colisusedt ogivecolor
stothebarsi nt
hegraph.

#Cr eatethedat aforthechar


t.
H<-c( 7,12,
28,3,41)
#Gi vethechar tfi
leaname.
png(fil
e=" barchart.
png")
#Pl otthebarchar t.
barplot(
H)
#Sav et hefi
le.
dev.off(
)

#Cr eatethedat aforthechar t.


H<-c( 7,12,
28,3,41)
M <-c("Mar "
,"
Apr "
,"
May "
,"Jun",
"Jul"
)
#Gi vethechar tfi
leaname.
png(fil
e=" barchart_mont hs_revenue.png")
#Pl otthebarchar t.
barplot(
H,names. arg=M, xlab="Month",y
lab="
Rev
enue"
,col
="bl
ue"
,
main="Rev enuechar t"
,bor der
="red")
#Sav et hefi
le.
dev.off(
)

#Cr eatetheinputv ector s.


colors<-c("
green","
or ange" ,
"brown" )
mont hs<-c("
Mar ",
"Apr ",
"May ","
Jun" ,
"Jul"
)
regions<-c("
East "
,"
West ","
Nor t
h" )
#Cr eatethemat ri
xoft hev alues.
Values<-mat ri
x(c(2,9,3,11,9,
4,8,7,
3,12,5,
2,8,
10,
11)
,nr
ow=3,
ncol
=5,
byr
ow=TRUE)
#Gi vethechartfil
eaname.
png(fil
e=" barchart
_stacked.png")
#Cr eatet hebarchar t.
barplot(
Val ues,main="tot
al
rev
enue" ,names. arg=mont hs,
xlab="month",
yl
ab="
rev
enue"
,col
=col
ors)
#Addt hel egendt othechar t
.
l
egend( "topleft"
,regi
ons,cex=1. 3,
fil
l=col
ors)
#Sav et hef i
le.
dev.off(
)

#--
--
---
---
--
--
---
--
--
--
--
--
--
--
--
---
--
---
--
--
--
--
--
---
---
--
--
--
--
--
--
--
--
--
--
--
-
# Pi
eChar t
s
#--
--
---
---
--
--
---
--
--
--
--
--
--
--
--
---
--
---
--
--
--
--
--
---
---
--
--
--
--
--
--
--
--
--
--
--
-
Syntax
Thebasi csyntaxforcreati
ngapi e-chartusi
ngtheRis:
pi
e(x,labels,r
adius,main,col,
clockwise)

Foll
owingi sthedescr i
pti
onoft heparamet ersused:
xisav ectorcontai
ningthenumer i
cv aluesusedi nthepiechar
t.
labelsisusedt ogivedescripti
ontot heslices.
radiusindicatestheradiusofthecircleoft hepiechar
t.
(val
uebetween-
1and+1).
mai nindicatesthetit
leofthechart.
colindicatesthecolorpalette.
clockwi seisalogicalval
ueindicat
ingi ft
hesl i
cesaredrawnclockwi
seorant
icl
ockwi
se.

#Av er
ysi
mplepi
e-char
tiscreat
edusingj
ustt
heinputvectorandl
abel
s.Thebel
owscr
iptwi
l
l
cr
eat
eandsav
ethepiecharti
nthecur
rentRwor
kingdir
ector
y .

#Createdat aforthegraph.
x<-c(21,62, 10,53)
l
abels<-c( "London","
NewYor k"
,"Si
ngapor
e",
"Mumbai
")
#Givethechar tfi
l
eaname.
png(f
il
e=" cit
y.j
pg")
#Plotthechar t
.
pi
e(x,
labels)
#Savet hef il
e.
dev.
off()

#Thebel
owscr
iptwi
l
lcr
eat
eandsav
ethepi
echar
tint
hecur
rentRwor
kingdi
rect
ory
.

#Givethechartfil
eaname.
png(f
il
e=" ci
ty_
titl
e_col
ours.
jpg"
)
#Plotthechartwithtit
leandrai
nbowcolorpal
l
et.
pi
e(x,l
abels,
mai n="Ci
typiechar
t",
col
=rai
nbow(l
engt
h(x)
))
#Savet hefi
le.
dev.
off(
)

#Wecanaddsl
i
ceper
cent
ageandachar
tlegendbycr
eat
ingaddi
ti
onal
char
tvar
iabl
es.

#Createdat aforthegraph.
x<-c(21,62,10,53)
l
abels<-c("London","
NewYor k",
"Si
ngapore"
,"
Mumbai
")
pi
epercent<-round(100*x/sum(x),
1)
#Givethechar tfi
leaname.
png(
fil
e=" cit
y_percentage_l
egends.j
pg"
)
#Plotthechar t
.
pi
e(x,
label
s=pi
eper
cent
,mai
n="
Cit
ypi
echar
t"
,col
=rai
nbow(
lengt
h(x)
))

l
egend("topr
ight
",c(
"London"
,"
NewYor
k",
"Si
ngapor
e",
"Mumbai
")
,cex=0.
8,
fi
ll
=rai
nbow(lengt
h(x))
)
#Sav ethefi
le.
dev.of
f()

#Apiechar
twith3di
mensionscanbedr
awnusi
ngaddi
ti
onal
packages.Thepackagepl
otr
ixhas
af
uncti
oncall
edpi
e3D()t
hatisusedf
ort
his.

#Gett hel i
brary.
l
ibrary(plotri
x)
#Cr eatedat af orthegraph.
x<-c( 21, 62,10,53)
l
bl <-c("London" ,"
NewYor k",
"Si
ngapor
e",
"Mumbai
")
#Gi vet hechar tfi
leaname.
png( f
il
e=" 3d_pie_char
t.j
pg")
#Pl otthechar t
.
pie3D(x,labels=lbl
,expl
ode=0.1,
mai n="PieChar tofCount r
ies")
#Sav et hef i
le.
dev .
off()

#----
--
---
--
----
--
----
---
--
---
---
--
---
---
--
---
---
--
--
--
--
--
--
---
---
--
--
--
--
--
---
-
# Boxplots
#----
--
---
--
----
--
----
---
--
---
---
--
---
---
--
---
---
--
--
--
--
--
--
---
---
--
--
--
--
--
---
-
Thebasi csy ntaxtocr eat eaboxpl otinRi s:
boxpl ot(
x,dat a,
notch, v
ar width,names,main)
Followingi st hedescr iptionoft heparamet er
sused:
xisav ect ororaf ormul a.
dataist hedat af rame.
notchisal ogical val
ue.SetasTRUEt odr awanot ch.
varwidthisal ogicalvalue.Setast ruet odrawwi dthoft
heboxproport
ionat
eto
thesampl esi ze.
namesar et hegr oupl abelswhi chwillbeprint
edundereachboxplot
.
maini susedt ogi veat itl
et othegraph.

i
nput<-mtcars[
,
c('
mpg'
,
'cy
l'
)]
pr
int
(head(
input
))

#Givethechar tfi
l
eaname.
png(f
il
e=" boxplot
.png")
#Plotthechar t.
boxpl
ot(mpg~cy l
,data=mtcar
s,
xl
ab="NumberofCy l
inders"
,
yl
ab="MilesPerGal lon",
main="MileageDat a")
#Savet hef i
le.
dev.
off(
)

#-
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
# Histr
ograms
#-
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
Synt
ax
Thebasicsy ntaxforcreati
ngahi stogr am usi
ngRi s:
hi
st(v,
main,xl
ab,xl
im,yl
im,breaks,col,bor der
)
Fol
lowingist hedescri
ptionoft hepar amet er
sused:
visav ectorcontaini
ngnumer icv aluesusedi nhist
ogr
am.
mainindicatestit
leofthechar t.
colisusedt osetcoloroft hebar s.
borderisusedt osetbor dercolorofeachbar .
xl
abi susedt ogivedescr i
ptionofx- axis.
xl
imi susedt ospecifyther angeofv al
uesont hex-
axi
s.
yl
imi susedt ospecifyther angeofv al
uesont hey-
axi
s.
breaksisusedt oment iont hewi dthofeachbar .

#Createdataf orthegr aph.


v<-c(9,
13,21,8,36,
22,12,41,31,
33,
19)
#Givethechar tfil
eaname.
png(
fil
e=" hi
st ogram.png")
#Createthehi stogr
am.
hi
st(
v,xl
ab="Wei ght"
,col="yell
ow",
bor
der
="bl
ue"
)
#Savethef i
le.
dev.
off(
)

#Cr eatethehi
stogr
am.
hi
st(v,
xlab="Wei
ght"
,col
="gr
een"
,bor
der
="r
ed"
,xl
i
m =c(
0,40)
,yl
i
m =c(
0,5)
,
breaks=5)
#Sav ethefil
e.
dev.off
()

#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
# Scat
terpl
ots
#-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--

Thebasi csy ntaxforcreatingscatt


erplotinRi s:
pl
ot(x,y ,
mai n,xlab,yl
ab,xlim,yl
im,axes)
Fol
lowi ngist hedescript
ionoft heparamet ersused:
xist hedat asetwhosev aluesarethehor izontalcoordi
nat
es.
yist hedat asetwhosev aluesarethev erti
cal coordi
nat
es.
mai nist het i
l
eoft hegraph.
xl
abi st helabel i
nthehor izont
alaxi
s.
yl
abi st helabel i
nthev erti
calaxi
s.
xl
imi sthelimitsofthev aluesofxusedf orplotti
ng.
yl
imi st helimitsofthev aluesofyusedf orpl otti
ng.
axesi ndicateswhet herbot haxesshouldbedr awnont heplot
.

i
nput<-mtcars[
,
c('
wt'
,
'mpg'
)]
pr
int
(head(
input
))

#Plotthechar tforcarswit
hweightbet
ween2.
5to5andmi
l
eagebet
ween15
and30.
pl
ot(x=i
nput $wt,
y=input$mpg,
xl
ab="Wei ght"
,
yl
ab="Mil
age" ,
xl
im=c(2.5,5),
yl
im=c(15,30),
main="Wei ghtvsMi l
age"
)
#Savethef
il
e.
dev
.of
f()

#Plotthemat ri
cesbet
ween4v ar
iabl
esgiv
ing12plot
s.
#Onev ariablewith3othersandt
otal4var
iabl
es.
pai
rs(~wt+mpg+di sp+cyl
,dat
a=mtcar
s,
main="Scatterpl
otMatri
x")
#Savet hefile.
dev
.off()

ht
tp:
//www.
r-
graph-
gal
l
ery
.com/
por
tfol
i
o/ggpl
ot2-
package/

You might also like