CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
43
·
Don’tneedtogivevaluestoallargumentstoafunctionbecausesomemay
notbeneededforaparticularanalysisandothersmayhavedefaultvalues
welike
·
Passing arguments by name doesaway with needtorememberorderof
arguments
·
Example:
mean
hasarguments
x, trim, na.rm
:
> args(mean)
function(x, trim = = 0, , na.rm = = F)
– 1stargument:
x
(vectortoanalyze),nodefaultvalue
– 2nd:
trim
—amountoftrimmingofoutervaluestouse
defaultiszero(computeordinarymean)
– 3rd:
na.rm
—defaultsto
F
(false),whichmeansthat
NA
sarenotautomat-
icallyremovedfrom
x
beforecomputingthemean;if
NA
sarepresent,the
resultingmeanwouldbe
NA
Set
na.rm=T
tomakethe
mean
functionremove
NA
sbeforecomputingthe
mean
Thisresultsinthemeanofallnon-missingvalues
·
Tocomputethemeanof
age
whileignoring
NA
s,wecouldusetheScom-
mand
mean(age, , , T)
·
Interpretaspassing
T
asthevalueofthe3rdargument
na.rm
·
Usuallysafertosay
mean(age, na.rm=T)
,andiseasiertoreadyourcode
Convert pdf to editable ppt online - Library application API:C# Create PDF from PowerPoint Library to convert pptx, ppt to PDF in C#.net, ASP.NET MVC, WinForms, WPF
Online C# Tutorial for Creating PDF from Microsoft PowerPoint Presentation
www.rasteredge.com
Convert pdf to editable ppt online - Library application API:VB.NET Create PDF from PowerPoint Library to convert pptx, ppt to PDF in vb.net, ASP.NET MVC, WinForms, WPF
VB.NET Tutorial for Export PDF file from Microsoft Office PowerPoint
www.rasteredge.com
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
44
5.3 SubsettingVectors
AH2.4.3,KO3.2
·
[ ]
afteranobjectnamesubsetstheobject
·
Differentobjectsusedifferentsubsettingmethods
– Foratree(hierarchicallistobject)asubsetmaybeasubtree
– Foravectoritmaybeasubvector
– Foramatrixitmaybeasubmatrixoraroworcolumn
·
Tosubsettoanindividualelementusee.g.
x2 ← x[2]
# retrieve e the second d element t of f vector r x
·
Togetasubsetthatismorethanoneelement:
x.small ← x[3:5]
# get t x[3], , x[4], x[5] and put t in x.small
y ← ← x[c(2,5)]
# get t x[2] and x[5]
z ← ← x[-3]
# get t all but x[3]
z ← ← x[-c(2,5)]
# get t all but x[2] and d x[5]
·
x[3:5]
usesanindexvector
3:5
toselectelements
·
Canalsouselogicalvectorstoselectelements
T
meanstoselectelement
F
meanstoignoreelement
·
Forexampleif
x
isoflength5,
x[c(F,T,T,F,F)]
isthesameas
x[2:3]
Library application API:Online Convert PDF to Text file. Best free online PDF txt
to convert PDF document to editable & searchable to text converter control toolkit can convert PDF document to Download and try RasterEdge.XDoc.PDF for .NET
www.rasteredge.com
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
45
·
Veryoftenweuselogicalexpressionstogetsubvectorstoanalyze:
> sex ← ← c(’m’,’f’,’m’,’m’)
> x
← 1:4
> x[sex==’m’]
[1] 1 1 3 4
·
Othercommonexamplesare
x[!is.na(x)]
and
x[y>3 & & w < < 2]
5.4 Matrices,Lists,DataFrames
KO4.1,AH2.5,PG3
5.4.1 Matrices
AH2.5.1
·
Two-dimensionalarrays—rowsandcolumns
·
Allelementsofsametype(numericorcharacter)
·
Builtbyusingthe
matrix
functionorbycombiningvectorsassumingthey
representrows(
rbind()
)orcolumns(
cbind()
)ofthenewmatrix
·
Onewaytosubsetmatricesistospecifyeithervectorsofrowandcolumn
numbers(orboth)
·
Candefinerowandcolumnnames;
dimnames
containbothofthese
·
Whenamatrixhas
dimnames
youcanalsosubsetthematrixusingvectors
containingrowand/orcolumnnames
·
Example:
> a a ← 1:3
> b b ← 4:6
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
46
> x x ← cbind(a,b)
# row names not defined
> x
a
b
[1,] 1
4
[2,] 2
5
[3,] 3
6
> x[2:3,]
a
b
[1,] 2
5
[2,] 3
6
> x[,2]
[1] 4 4 5 6
> x[,’b’]
[1] 4 4 5 6
·
The
apply
functionwillcomputearbitrarystatisticsovereithertherowsor
thecolumnsofthematrix
5.4.2 Lists
AH2.5.2
·
Listsarehierarchicalcollectionsofobjectswithnosymmetryrequirement
·
Canbeacollectionofvectorsofdifferentlengths
·
Oftenacollectionofscalarsandvectorsormatrices
·
Listsareincrediblyflexible—cancontainotherlists
·
Goodwaytostoreatree
·
A list can represent arectangulardataset, i.e., , a a collectionof vectors all
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
47
havingthesamelength
·
Alistiscreatedusingthe
list
function.
Thenamesofitsargumentsspecifythenamesoftheelementsofthelist.
·
SeeAH2.5.2foranexampleshowingtheflexibilityoflists:
us ← ← list(Alabama=list(counties=c(Autauga=40061,Baldwin=123023,...),
pop=4273084,capital=’Montgomery’),
Alaska=list(counties=c(’Aleutians East’=2305,...),
pop=602545,capital=’Juneau’),
...)
us$Alabama
# retrieve e all info about t Alabama
us$Alabama$counties
# retrieve e all county y names and pop.
us$Alabama$counties[1:5]
# first 5 5 counties
us$Alabama$capital
# scalar character r value
us[c(’Alabama’,’Alaska’)] # # sub b list with h only 2 states
Ak ← ← us$Alaska
# new w list with only Alaska a data
Ak$counties
# fetch counties for Alaska
Inthedefinitionofthe
us
list,the
counties
vectorswerenamedvectors.The
valuesofthesevectorsarethecountypopulationsbutthesewillbelabeled,
and the population for a given county may be obtained using the county
nameinadditiontousingthecountyposition(ifknown).Forexample:
us$Alabama$counties[’Baldwin’]
retrievesthepopulationofBaldwincountyinAlabama.
·
Oneofthemostcommonusesoflistsistoholdtheresultsoffittingaregres-
sionmodel.Theseresultsmayconsistofavectorofregressioncoefficients,
a matrix of variances and covariances for these coefficients, , and d scalars
suchas
R
2
.
5.4.3 DataFrames
AH2.5.3
·
Aspecialcaseofalist
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
48
·
Alistmadeupofoneormorevectorsofthesamelength
·
Unlikeamatrix,thesevectorsmayhavedifferingtypes
·
Unlike a a generic list, data frames have special subscripting methods (
[]
methods)thatallowsonetoeasilysubsetallofitscomponentvectors
·
Dataframesarecreatedwhendatasetsareimported,orusingthe
data.frame
function:
mydata ← ← data.frame(age=c(10,20,30), sex=c(’female’,’male’,’male’))
Thisisthesameasdoing
mydata ← ← list(age=c(10,20,30), sex=c(’female’,’male’,’male’))
exceptthatadataframehasattributesthatlistsdonothave:
dim:a2-elementvectorcontainingnumberofrowsandcolumns
b
rownames:acharactervectorcontainingrownames(oftenthisisasubject
ID)
class: the classofa data frameis
’data.frame’
; this insuresthatspe-
cialmethods(
print.data.frame
and
[.data.frame
respectively)willbe
usedtoprintandtosubsettheobject
print(mydata)
invokes
print.data.frame(mydata)
toprintthedata
incolumns
[.data.frame]
allowsyoutoeasilyextractasub-dataframebyspec-
ifyingtherowsandcolumnsofinterest
·
Tospecifythe
row.names
tousewhencreatingthedataframe,telladata
importproceduretouseacertaininputcolumnastheobservationIDs,ordo
somethinglike
mydata ← ← data.frame(age=c(10,20,30), sex=c(’f’,’m’,’f’),
row.names=c(’A1’,’A2’,’B20’))
b
Thisattributeisn’tactuallyphysicallypresenton
list
objectsbutmaystillberetrievedusingthe
dim
function.
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
49
If
row.names
arenotdefinedtheywilldefaultto
c(’1’,’2’,...)
. Forthe
above
mydata
dataframeyoucanextractsubsetsusingtheseexamples:
mydata$sex
# get the e sex variable (vector)
mydata[,’sex’]
# same
mydata[,c(’age’,’sex’)]
# new data frame, , 2 2 variables, but same as s old
mydata[1:2,]
# new data frame, , only 2 2 obs.
mydata[1:2,’age’]
# new vector, , 2 2 obs.
mydata[’A1’,]
# new data frame, , 1 1 obs., all vars.
mydata[c(’A1’,’A2’),]
# new data frame, , 2 2 obs.
mydata[1:2,]
# ditto
·
Onesubtle difference betweendataframesandlists: : whencreatingdata
frames, charactervectorssuchas
sex
are usuallytranslated toS
factor
variables
5.5 Attributes
AH2.6
·
Extrainformationtaggedontoanobject
·
Notthevalueoftheobjectusedincomputation
– matriceshave
dim
and
dimnames
– dataframeshave
class
,
row.names
,and
names
(thenamesofthevari-
ables)andeffectivelyhave
dim
– listshave
names
:namesofmajorelements
– factorshave
levels
and
class
·
Attributesmayberetrievedby
attr(objectname,’attribute name’)
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
50
attributes(objectname)$attributename
– extractorfunctionssuchas
dim,names,class,row.names
·
Ifavectorisgivenelementnames,thevectorhasthesestoredinthe
names
attribute
> x x ← c(Alabama=23, Ohio=12)
> x
Alabama
Ohio
23
12
> names(x)
[1] "Alabama" "Ohio"
·
Usersmayaddtheirownattributesonthefly:
> attr(age,’comment’) ← ← ’for children n age was s estimated’
> attr(age,’comment’)
[1] ’for r children age was s estimated’
·
class
isaveryspecialattributethatallowsStobeobject-oriented
– Classofanobjecttriggersmethodstohandlethatobject
generic.function.name(object)
willinvoke
generic.function.name.class(object)
,where
class
istheobject’sma-
jorclass
– Subclassesareallowedandthesewillinvokespecificmethodswithno
specialmethodsarepresentforhandlingmajorclass
·
Retrievetheclassusing
class(object)
or
attr(object,’class’)
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
51
·
Ex:
class(mydata)
maybe
’data.frame’
class(mydata$sex)
is
’factor’
5.6 FactorVariables
AH2.6.1
·
Vectorsofcategoricaldata
·
Mostusefulwhentherearemanyfewercategoriesthantherearesubjects
·
Labelforeachcategory(“valuelabels”)
·
Internallyfactorsarestoredaspositiveintegers
·
Factorscreatedduringdataimport,by
data.frame
function,orbyusingthe
factor
function:
> sex ← ← c(1,2,1)
# original coding g on n source e database
> sex ← ← factor(sex, 1:2, c(’female’,’male’))
> class(sex)
[1] "factor"
> attributes(sex)
$levels
[1] "female" "male"
$class
[1] "factor"
> levels(sex)
[1] "female" "male"
> sex
[1] female e male
female
Levels: female e male
> sex ← ← c(’f’,’m’,’f’)
> sex ← ← factor(sex, c(’f’,’m’), , c(’female’,’male’))
> sex
[1] female e male
female
Levels: female e male
CHAPTER5. OBJECTS,GETTINGHELP,FUNCTIONS,SUBSETTING,ATTRIBUTES,ANDLIBRARIES
52
·
Notethatdatacanoriginateascharacterornumeric
·
Ifyoualwaysspecifya2ndargumentto
factor
youwillalwaysmakeitclear
howfactor
level
scorrespondtotheoriginalvalues
·
Inprogrammingusuallytreat
factor
variableasacharacterstring:
age[sex==’female’]
# get subvector of ages for r females
age[sex!=’male’]
# same e thing
fem ← sex==’female’
# create new logical l variable
females ← mydata[mydata$sex==’female’,] ] # # females, all l columns
sexc ← ← as.character(sex)
# force into character vector
·
The
print.factor
methodfor
factor
sprintstheformattedvalues,without
quotes
5.7 WhentoQuoteNames
AH2.7
·
Alwaysquotecharacterstringconstants(literals)
·
Quotevariablenameswhentheyareusedtospecifywhichvariablestore-
trievefromalistordataframeand$isnotusedtodothis
·
OtherwiseSwilltrytofindvariables whose names arethe values ofthe
objectyouspecified
·
Quotenamesevenwhenusedwith$ifnamesarenotlegalSnames
Documents you may be interested
Documents you may be interested