Repeat Character String N Times in R

• Last Updated : 03 Sep, 2021

In this article, we will discuss how to repeat the character string N times in the R programming language. Character string means a set of characters .

Example:

“Hello Geek”,”Python”,”Languages_Python” are some examples

Method 1: Using replicate() method

This function used to give n replicas from the character string

Syntax:

replicate(N, “string”)

where,

• N is the number of times string is replicated
• string is the input character string

Example: R program to repeat the character string N times using replicate

R

 # get 2 timesprint(replicate(2, "Hello_Geek")) print("-----") # get 10 timesprint(replicate(10, "Python")) print("-----") # get 3 timesprint(replicate(3, "java")) print("-----") # get 4 timesprint(replicate(4, "dbms")) print("-----") # get 5 timesprint(replicate(5, "sql")) print("-----") # get 7 timesprint(replicate(7, "big data"))

Output:

 “Hello_Geek” “Hello_Geek”

 “—–“

 “Python” “Python” “Python” “Python” “Python” “Python” “Python” “Python”

 “Python” “Python”

 “—–“

 “java” “java” “java”

 “—–“

 “dbms” “dbms” “dbms” “dbms”

 “—–“

 “sql” “sql” “sql” “sql” “sql”

 “—–“

 “big data” “big data” “big data” “big data” “big data” “big data” “big data”

Method 2: Using rep() method

This function works similar to replicate .

Syntax:

rep( “string”,N)

Example: R program that repeats character String N times using rep

R

 # get 2 timesprint(rep("Hello_Geek", 2)) print("-----") # get 10 timesprint(rep("Python", 10)) print("-----") # get 3 timesprint(rep("java", 3)) print("-----") # get 4 timesprint(rep("dbms", 4)) print("-----") # get 5 timesprint(rep("sql", 5)) print("-----") # get 7 timesprint(rep("big data", 7))

Output:

 “Hello_Geek” “Hello_Geek”

 “—–“

 “Python” “Python” “Python” “Python” “Python” “Python” “Python” “Python”

 “Python” “Python”

 “—–“

 “java” “java” “java”

 “—–“

 “dbms” “dbms” “dbms” “dbms”

 “—–“

 “sql” “sql” “sql” “sql” “sql”

 “—–“

 “big data” “big data” “big data” “big data” “big data” “big data” “big data”

Method 3 : Using paste along with replicate

This paste is used to organize the repeated strings in the correct way, It will separate the strings with the given delimiter.

Syntax:

paste(replicate(N, “string”), collapse = “delimiter”)

where,

• paste is used to display the data
• replicate is used to get the N character strings
• collapse is sued to separate the strings

Example: R program to repeat the character strings using paste command

R

 # get 2 times with delimiter --paste(replicate(2, "Geek"), collapse = "--") print("-----") # get 10 times with delimiter ,paste(replicate(2, "Python"), collapse = ",")

Output:

 “Geek–Geek”

 “—–“

 “Python,Python”

Method 4: Using strrep() function

This function is used to get the N character strings in a single string.

Syntax:

strrep( “string”,N)

Example: R program to get N character strings using strrep() function

R

 # get 2 timesprint(strrep( "Hello_Geek",2)) print("-----") # get 10 timesprint(strrep( "Python",10)) print("-----") # get 3 timesprint(strrep( "java",3)) print("-----") # get 4 timesprint(strrep("dbms",4)) print("-----") # get 5 timesprint(strrep( "sql",5)) print("-----") # get 7 timesprint(strrep("big data",7))

Output:

 “Hello_GeekHello_Geek”

 “—–“

 “PythonPythonPythonPythonPythonPythonPythonPythonPythonPython”

 “—–“

 “javajavajava”

 “—–“

 “dbmsdbmsdbmsdbms”

 “—–“

 “sqlsqlsqlsqlsql”

 “—–“

 “big databig databig databig databig databig databig data”

My Personal Notes arrow_drop_up