Solution Review: Strings

In this review, we give a detailed analysis of the solution to this problem.

Solution #1: Using rep()

Press + to interact
testVariable <- "learning is fun and easy"
cat(rep(testVariable, 3))

Explanation

Simply use the keyword rep() with the testVariable. Remember that the first argument to rep() is the string that needs to be duplicated and then the number of times it needs to be duplicated. Pass this whole set into the cat() keyword for printing.

Solution #2: Using replicate()

Press + to interact
testVariable <- "learning is fun and easy"
cat(replicate(3, testVariable))

Explanation

We can also use replicate() to perform the same task. Here, the first argument is the number of times duplication needs to be performed and the second argument is the string to be replicated. Later, for printing, pass this to cat().