How to replicate elements of vectors and lists in R
Overview
The rep function in R replicates the values in a given vector.
Syntax
The rep function takes the following syntax:
rep.int(x, times)
The syntax for the rep() function in R
Parameter value
The rep() function takes the following parameter values:
x: This is the input vector whose values are to be replicated.times: This specifies the number of times the value ofxhas been replicated.
Return value
The rep() function returns an object of the same type as the input vector x.
Example
# A code to illustrate the rep() function in R# creating a vector objecta <- 1:5# replicating the values in a by 2 timesb <- rep(a, 2)b
Explanation
- Line 4: We create a vector object,
a. - Line 7: We use the
rep()function to create a replicate of the values ofa. The result is assigned to a variable,b. - Line 9: We print the value of
b.