Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Replicating a Value specified number of times in R Programming - rep.int() Function
Next article icon

Replicate elements of vector in R programming – rep() Method

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In the R programming language, A very useful function for creating a vector by repeating a given numbervector with the specified number of times is the rep().

The general structure of rep() : rep(v1,n1).

Here, v1 is repeated n1 times.

R – replicate elements of vector

The forms of rep() functions :

  • rep(v1, times=)
  • rep(v1, each=)
  • rep(v1, length=)

Example 1:

R

# Replicate '0' 5 time
rep(0, 5)
                      
                       

Output:

[1] 0 0 0 0 0

Example 2:

rep(v1,  times=)

R

# 1,2,3 repeated 3 times in sequencially
rep(1:3,times=3)
                      
                       

Output:

[1] 1 2 3 1 2 3 1 2 3

Example 3:

rep(v1,  each=)

R

# 1,2,3 repeated 3 times
rep(1:3,each=3)
                      
                       

Output:

[1] 1 2 3 1 2 3 1 2 3

Example 4:

rep(v1,  length=)

R

# generate a vector 1,2,3
x<-1:3
 
# vector x is replicated such that the
# length is five.
rep(x, length=5)
                      
                       

Output:

1 2 3 1 2

Example 5:

R

# 1 is replicated 2 times, and so on
rep(x,c(2,1,3))
                      
                       

Output:

1 1 2 3 3 3


Next Article
Replicating a Value specified number of times in R Programming - rep.int() Function

J

Jitender_1998
Improve
Article Tags :
  • R Language

Similar Reads

  • Adding elements in a vector in R programming - append() method
    append() method in R programming is used to append the different types of integer values into a vector in the last. Syntax: append(x, value, index(optional)) Return: Returns the new vector after appending given value. Example 1: x <- rep(1:5) # Using rep() method gfg <- append(x, 10) print(gfg
    1 min read
  • Creating a Vector of sequenced elements in R Programming - seq() Function
    In This article, we will discuss how we Create a Vector of sequenced elements in R Programming Language using seq() Function. What are sequenced elements?Sequenced elements mean things that are placed in a particular order, one after another. This concept is often used in various fields. seq() Funct
    2 min read
  • Types of Vectors in R Programming
    Vectors in R programming are the same as the arrays in C language which are used to hold multiple data values of the same type. One major key point is that in R the indexing of the vector will start from ‘1’ and not from ‘0’. Vectors are the most basic data types in R. Even a single object created i
    5 min read
  • Calculate Rank of the Values of a Vector in R Programming - rank() Function
    rank() function in R Language is used to return the sample ranks of the values of a vector. Equal values and missing values are handled in multiple ways. Syntax: rank(x, na.last) Parameters: x: numeric, complex, character, and logical vector na.last: Boolean Value to remove NAs Example 1: # R progra
    1 min read
  • Replicating a Value specified number of times in R Programming - rep.int() Function
    rep.int() function in R Language is used to replicate a given value into a specified number of times. Syntax: rep.int(x, times) Parameters: x: specified value times: specified number of times the value get printed Example 1: # R program to illustrate # rep.int function # Calling the rep.int() functi
    1 min read
  • Replicating a Value to the Specified Length in R Programming - rep_len() Function
    rep_len() function in R Language is used to replicate a given value in the specified length. Syntax: rep_len(x, length.out) Parameters: x: specified value length.out: specified number of items get printed Example 1: # R program to illustrate # rep_len function # Calling the rep_len() function rep_le
    1 min read
  • Repeatedly Evaluate an Expression in R Programming - replicate() Function
    replicate() function in R Language is used to repeatedly evaluate a function or expression. It is member of apply family in R base package. In this article, we'll learn syntax and implementation of replicate() function with the help of examples. Syntax: replicate(n, expr, simplify) Parameters: n: re
    1 min read
  • Get or Set the Structure of a Vector in R Programming - structure() Function
    structure() function in R Language is used to get or set the structure of a vector by changing its dimensions. Syntax: structure(vec, dim) Parameters: vec: Vector dim: New Dimensions Example 1: # R program to get # the structure of a Vector # Creating a Vector # of Numbers x1 <- c(1:10) structure
    1 min read
  • Dot Product of Vectors in R Programming
    In mathematics, dot product or commonly referred as the scalar product is an algebraic operation involving the two same-length sequences of numbers and a resultant single number. Let us assume two vectors A and B and we have to calculate the dot product of two vectors. Formula for Dot Product[Tex]Do
    3 min read
  • Append Operation on Vectors in R Programming
    In this article, let us discuss different methods to concatenate/append values to a vector in R Programming Language.  Append method in RVectors in the R Programming Language is a basic objects consisting of sequences of homogeneous elements. vector can be integer, logical, double, character, comple
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences