subset function - RDocumentation

文章推薦指數: 80 %
投票人數:10人

Return subsets of vectors, matrices or data frames which meet conditions. ... in recent versions of R this can simply be subset(state.x77, grepl("^M", nm), ... base(version3.6.2)DescriptionReturnsubsetsofvectors,matricesordataframeswhichmeetconditions.Usagesubset(x,…)#S3methodfordefault subset(x,subset,…)#S3methodformatrix subset(x,subset,select,drop=FALSE,…)#S3methodfordata.frame subset(x,subset,select,drop=FALSE,…)Argumentsxobjecttobesubsetted.subsetlogicalexpressionindicatingelementsorrowstokeep: missingvaluesaretakenasfalse.selectexpression,indicatingcolumnstoselectfroma dataframe.droppassedonto[indexingoperator.…furtherargumentstobepassedtoorfromothermethods.ValueAnobjectsimilartoxcontainjusttheselectedelements(for avector),rowsandcolumns(foramatrixordataframe),andsoon.WarningThisisaconveniencefunctionintendedforuseinteractively.For programmingitisbettertousethestandardsubsettingfunctionslike [,andinparticularthenon-standardevaluationof argumentsubsetcanhaveunanticipatedconsequences.DetailsThisisagenericfunction,withmethodssuppliedformatrices,data framesandvectors(includinglists).Packagesanduserscanadd furthermethods. Forordinaryvectors,theresultissimply x[subset&!is.na(subset)]. Fordataframes,thesubsetargumentworksontherows.Note thatsubsetwillbeevaluatedinthedataframe,socolumnscan bereferredto(byname)asvariablesintheexpression(seetheexamples). Theselectargumentexistsonlyforthemethodsfordataframes andmatrices.Itworksbyfirstreplacingcolumnnamesinthe selectionexpressionwiththecorrespondingcolumnnumbersinthedata frameandthenusingtheresultingintegervectortoindexthe columns.Thisallowstheuseofthestandardindexingconventionsso thatforexamplerangesofcolumnscanbespecifiedeasily,orsingle columnscanbedropped(seetheexamples). Thedropargumentispassedontotheindexingmethodfor matricesanddataframes:notethatthedefaultformatricesis differentfromthatforindexing. Factorsmayhaveemptylevelsaftersubsetting;unusedlevelsare notautomaticallyremoved.Seedroplevelsforawayto dropallunusedlevelsfromadataframe.SeeAlso[, transform droplevelsExamples#NOTRUN{ subset(airquality,Temp>80,select=c(Ozone,Temp)) subset(airquality,Day==1,select=-Temp) subset(airquality,select=Ozone:Wind) with(airquality,subset(Ozone,Temp>80)) ##sometimesrequiringalogical'subset'argumentisanuisance nm



請為這篇文章評分?