Repeat Character String N Times in R
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
print ( replicate (2, "Hello_Geek" ))
print ( "-----" )
print ( replicate (10, "Python" ))
print ( "-----" )
print ( replicate (3, "java" ))
print ( "-----" )
print ( replicate (4, "dbms" ))
print ( "-----" )
print ( replicate (5, "sql" ))
print ( "-----" )
print ( replicate (7, "big data" ))
|
Output:
[1] “Hello_Geek” “Hello_Geek”
[1] “—–“
[1] “Python” “Python” “Python” “Python” “Python” “Python” “Python” “Python”
[9] “Python” “Python”
[1] “—–“
[1] “java” “java” “java”
[1] “—–“
[1] “dbms” “dbms” “dbms” “dbms”
[1] “—–“
[1] “sql” “sql” “sql” “sql” “sql”
[1] “—–“
[1] “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
print ( rep ( "Hello_Geek" , 2))
print ( "-----" )
print ( rep ( "Python" , 10))
print ( "-----" )
print ( rep ( "java" , 3))
print ( "-----" )
print ( rep ( "dbms" , 4))
print ( "-----" )
print ( rep ( "sql" , 5))
print ( "-----" )
print ( rep ( "big data" , 7))
|
Output:
[1] “Hello_Geek” “Hello_Geek”
[1] “—–“
[1] “Python” “Python” “Python” “Python” “Python” “Python” “Python” “Python”
[9] “Python” “Python”
[1] “—–“
[1] “java” “java” “java”
[1] “—–“
[1] “dbms” “dbms” “dbms” “dbms”
[1] “—–“
[1] “sql” “sql” “sql” “sql” “sql”
[1] “—–“
[1] “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 used to separate the strings
Example: R program to repeat the character strings using paste command
R
paste ( replicate (2, "Geek" ), collapse = "--" )
print ( "-----" )
paste ( replicate (2, "Python" ), collapse = "," )
|
Output:
[1] “Geek–Geek”
[1] “—–“
[1] “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
print ( strrep ( "Hello_Geek" ,2))
print ( "-----" )
print ( strrep ( "Python" ,10))
print ( "-----" )
print ( strrep ( "java" ,3))
print ( "-----" )
print ( strrep ( "dbms" ,4))
print ( "-----" )
print ( strrep ( "sql" ,5))
print ( "-----" )
print ( strrep ( "big data" ,7))
|
Output:
[1] “Hello_GeekHello_Geek”
[1] “—–“
[1] “PythonPythonPythonPythonPythonPythonPythonPythonPythonPython”
[1] “—–“
[1] “javajavajava”
[1] “—–“
[1] “dbmsdbmsdbmsdbms”
[1] “—–“
[1] “sqlsqlsqlsqlsql”
[1] “—–“
[1] “big databig databig databig databig databig databig data”
Last Updated :
09 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...