Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

r
replicate element
communitycreator

How to replicate elements of vectors and lists in R

Onyejiaku Theophilus Chidalu

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 of x has 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 object
a <- 1:5

# replicating the values in a by 2 times
b <- rep(a, 2)

b
Implement the rep() function

Explanation

  • Line 4: We create a vector object, a.
  • Line 7: We use the rep() function to create a replicate of the values of a. The result is assigned to a variable, b.
  • Line 9: We print the value of b.

RELATED TAGS

r
replicate element
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring