rep: Replicate Elements of Vectors and Lists - RDocumentation

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

rep replicates the values in x . It is a generic function, and the (internal) default method is described here. rep.int and rep_len are faster simplified ... base(version3.6.2)Descriptionrepreplicatesthevaluesinx.Itisageneric function,andthe(internal)defaultmethodisdescribedhere. rep.intandrep_lenarefastersimplifiedversionsfor twocommoncases.Internally,theyaregeneric,somethodscanbe definedforthem.Usagerep(x,…)rep.int(x,times)rep_len(x,length.out)Argumentsxavector(ofanymodeincludingalist)orafactoror(for reponly)aPOSIXctorPOSIXltorDate object;oranS4objectcontainingsuchanobject.…furtherargumentstobepassedtoorfromothermethods. Fortheinternaldefaultmethodthesecaninclude: timesaninteger-valuedvectorgivingthe (non-negative)numberoftimestorepeateachelementifof lengthlength(x),ortorepeatthewholevectorifof length1.NegativeorNAvaluesareanerror.A doublevectorisaccepted,otherinputsbeingcoercedto anintegerordoublevector. length.outnon-negativeinteger.Thedesiredlengthofthe outputvector.Otherinputswillbecoercedtoadouble vectorandthefirstelementtaken.IgnoredifNAorinvalid. eachnon-negativeinteger.Eachelementofx isrepeatedeachtimes.Otherinputswillbecoercedto anintegerordoublevectorandthefirstelementtaken.Treatedas 1ifNAorinvalid. times,length.outsee…above.ValueAnobjectofthesametypeasx. rep.intandrep_lenreturnnoattributes(exceptthe classifreturningafactor). Thedefaultmethodofrepgivestheresultnames(whichwill almostalwayscontainduplicates)ifxhadnames,butretains nootherattributes.DetailsThedefaultbehaviourisasifthecallwasrep(x,times=1,length.out=NA,each=1) .Normallyjustoneoftheadditional argumentsisspecified,butifeachisspecifiedwitheither oftheothertwo,itsreplicationisperformedfirst,andthenthat impliedbytimesorlength.out. Iftimesconsistsofasingleinteger,theresultconsistsof thewholeinputrepeatedthismanytimes.Iftimesisa vectorofthesamelengthasx(afterreplicationby each),theresultconsistsofx[1]repeated times[1]times,x[2]repeatedtimes[2]timesand soon. length.outmaybegiveninplaceoftimes, inwhichcasexisrepeatedasmanytimesasis necessarytocreateavectorofthislength.Ifbotharegiven, length.outtakespriorityandtimesisignored. Non-integervaluesoftimeswillbetruncatedtowardszero. Iftimesisacomputedquantityitisprudenttoaddasmall fuzzoruseround.Andanalogouslyforeach. Ifxhaslengthzeroandlength.outissuppliedandis positive,thevaluesarefilledinusingtheextractionrules,thatis byanNAoftheappropriateclassforanatomicvector (0forrawvectors)andNULLforalist.ReferencesBecker,R.A.,Chambers,J.M.andWilks,A.R.(1988) TheNewSLanguage. Wadsworth&Brooks/Cole.SeeAlsoseq,sequence,replicate.ExamplesRunthiscode#NOTRUN{ rep(1:4,2) rep(1:4,each=2)#notthesame. rep(1:4,c(2,2,2,2))#sameassecond. rep(1:4,c(2,1,2,1)) rep(1:4,each=2,len=4)#first4only. rep(1:4,each=2,len=10)#8integersplustworecycled1's. rep(1:4,each=2,times=3)#length24,3completereplications rep(1,40*(1-.8))#length7onmostplatforms rep(1,40*(1-.8)+1e-7)#better ##replicatealist fred



請為這篇文章評分?