g g g g
RogergD.gPeng
Thisgbookgisgforgsalegatghttp://leanpub.com/
rprogramminggThisgversiongwasgpublishedgong2015-04-
13
ThisgisgagLeanpubgbook.gLeanpubgempowersgauthorsgandgpublishersgwithgthegLeangPublishinggpr
ocess.gLeangPublishinggisgthegactgofgpublishinggangin-
progressgebookgusingglightweightgtoolsgandgmanygiterationsgtoggetgreadergfeedback,gpivotguntilgy
oughavegthegrightgbookgandgbuildgtractiongoncegyougdo.
©2014g-g2015gRogergD.gPeng
,Contents
Preface.............................................................................................................................................1
HistorygandgOverviewgofgR...........................................................................................................4
WhatgisgR?...................................................................................................................................4
WhatgisgS?...................................................................................................................................4
ThegSgPhilosophy.........................................................................................................................5
BackgtogR....................................................................................................................................5
BasicgFeaturesgofgR.....................................................................................................................6
FreegSoftware..............................................................................................................................6
DesigngofgthegRgSystem..............................................................................................................7
LimitationsgofgR..........................................................................................................................8
RgResources.................................................................................................................................9
GettinggStartedgwithgR.................................................................................................................11
Installation.................................................................................................................................11
GettinggstartedgwithgthegRginterface..........................................................................................11
RgNutsgandgBolts..........................................................................................................................12
EnteringgInput...........................................................................................................................12
Evaluation..................................................................................................................................12
RgObjects...................................................................................................................................13
Numbers....................................................................................................................................13
Attributes...................................................................................................................................14
CreatinggVectors.......................................................................................................................14
MixinggObjects..........................................................................................................................15
ExplicitgCoercion.......................................................................................................................15
Matrices.....................................................................................................................................16
Lists...........................................................................................................................................17
Factors.......................................................................................................................................18
MissinggValues..........................................................................................................................19
DatagFrames..............................................................................................................................20
Names........................................................................................................................................21
Summary...................................................................................................................................22
, GettinggDatagIngandgOutgofgR....................................................................................................23
ReadinggandgWritinggData.........................................................................................................23
ReadinggDatagFilesgwithgread.table()....................................................................................................23
ReadinggingLargergDatasetsgwithgread.table..............................................................................24
CalculatinggMemorygRequirementsgforgRgObjects....................................................................25
UsinggTextualgandgBinarygFormatsgforgStoringgData................................................................27
Usinggdput()gandgdump().............................................................................................................................27
BinarygFormats..........................................................................................................................29
InterfacesgtogthegOutsidegWorld.................................................................................................31
FilegConnections........................................................................................................................31
ReadinggLinesgofgagTextgFile.....................................................................................................32
ReadinggFromgagURLgConnection..............................................................................................33
SubsettinggRgObjects....................................................................................................................35
SubsettinggagVector...................................................................................................................35
SubsettinggagMatrix...................................................................................................................36
SubsettinggLists.........................................................................................................................37
SubsettinggNestedgElementsgofgagList........................................................................................38
ExtractinggMultiplegElementsgofgagList.....................................................................................39
PartialgMatching........................................................................................................................39
RemovinggNAgValues................................................................................................................40
VectorizedgOperations..................................................................................................................42
VectorizedgMatrixgOperations....................................................................................................43
DatesgandgTimes...........................................................................................................................44
DatesgingR.................................................................................................................................44
TimesgingR.................................................................................................................................44
OperationsgongDatesgandgTimes................................................................................................46
Summary...................................................................................................................................47
ControlgStructures........................................................................................................................48
if-else................................................................................................................................................................ 48
forgLoops.................................................................................................................................50
Nestedgforgloops.......................................................................................................................52
whilegLoops.............................................................................................................................53
repeatgLoops...........................................................................................................................54
next,gbreak........................................................................................................................................................54
Summary...................................................................................................................................55
Functions.......................................................................................................................................56
FunctionsgingR...........................................................................................................................56
YourgFirstgFunction....................................................................................................................56