How to Use rep() Function in R - R-Lang
文章推薦指數: 80 %
The rep() is a built-in generic R function that replicates the values in the provided vector. The rep() method takes a vector as an argument and ... Skiptocontent Menu AboutUs ContactUs TermsofService PrivacyPolicy Therep()isaniterationfunctioninR.Thetermiterationmeansrepetition. AniterationisacoreaspectofR.Theregularloopsarelikeforloop,whileloopiscostlyintimeandmemorymanagement. Therep()functionisagoodvectorizedalternativemethodwhosegoalistoachieveiteration.Vectorizedmethodsoperateonallthevectorelementsconcurrently,andvectorizedcomputationsalwaysgetfasterresults. repinR Therep()isabuilt-ingenericRfunctionthatreplicatesthevaluesintheprovidedvector. Therep()methodtakesavectorasanargumentandreturnsthereplicatedvalues.Thus,the rep()isavectorizedloopingfunctionwhoseonlygoalistoachieveiterationwithoutcostingtimeandmemory. Therep()functioninRhastwofaster-simplifiedversions. rep.int() rep_len() Syntax rep(rv,…) Parameters Therep()functiontakesamaximumoftwoarguments. The rv isavector(ofanymode,includingalist)orafactor. The(…) arefurtherargumentstobepassedtoorfromothermethods.Forexample,itcanbeoneofthefollowing. times:Itisaninteger-valuedvectorgivingthe(non-negative)numberoftimestorepeateachitemifoflength.Forexample,length(x),ortorepeatthewholevectorifoflength1. length.out: Itisanon-negativeinteger—thedesiredlengthoftheoutputvector. each:Itisanon-negativeinteger.Eachitemofxisrepeatedeachtime. Example rep(11,4) Output [1]11111111 Inthisexample,wearerepeatingvector11fourtimes. TorepeatNAvaluesmorethanonetime,usetherep()function. rep(NA,5) Output [1]NANANANANA RepeatcountingfromnumbertonumberinR Repeatthecountingnumbersfrom1to4threetimes. rep(1:4,3) Output [1]123412341234 Youcanseethat1through4isrepeatedfourtimes. RepeatvectorwithanincompletecycleinR Torepeatavectorwithanincompletecycle,usethe length.out argumentintherep()function. rep(1:4,3,length.out=9) Output [1]123412341 Byprovidinglength.outargument,youcanrestrictthelengthoftheoutput.So,forexample,youcanseethatthecycleisincompletebecauseoutputshouldconsistof12integers,butinstead,itcontains9integers. Passing each argumenttotherep()function Eachparameterisanon-negativeinteger.Thus,eachelementofxisrepeatedeachtime. rep(1:4,each=2) Output [1]11223344 Youcanseethateachelementfrom1to4isrepeated2times. Automatedlengthrepetition Avectorcanexpandtoabiasedpanelbyreplacingthelengthparameterwithavectorthatdefinesthenumberoftimeseachiteminthevectorwillrepeat. rep(1:4,1:4) Output [1]1223334444 Here,youcanseethat1appears1time,2twotimes,3threetimes,and4fourtimes. Usingtherep()functiontoreplicatealist Youcanusetherep()functiontoreplicatealistinR. data
延伸文章資訊
- 1R语言中rep函数的用法- 小鲨鱼2018 - 博客园
1、R语言中rep函数主要是重复输出:. 简单示例:. rep(1,4) ## 1重复4次[1] 1 1 1 1 · 2、each和time选项. 复制代码 · 3、向量的每个元素按照指定次数输出.
- 2Chapter 6 基本函式| R 資料科學與統計 - Bookdown
{R} 有許多函式(function), 函式是一種物件, 是指令的集合, 執行特定功能或運算工作的指令, ... {R} 有幾個產生數列的基本函式, 包含 : , sequence() , r...
- 3R rep Function Examples - EndMemo
R rep Function ... rep(x) function replicates the values x. ... x : numeric vector, factor, list ...
- 4R语言:rep函数解析_满腹的小不甘 - CSDN博客
R语言:rep函数解析 · x:代表的是你要进行复制的对象,可以是一个向量或者是一个因子。 · times:代表的是复制的次数,只能为正数。负数以及NA值都会为错误 ...
- 5R 筆記rep 函數 - 龍崗山上的倉鼠
在報告R 迴圈、迭代元素 該章中有個有趣的東西, 名為rep( )函數 它的用途為... rep( [ 指定的向量], [使之重複的次數]) 拿看文件後的Description.