In this Tutorial we will learn Repeat and Replicate function in R. Repeat and Replicate are import among the R functions.

### Repeat Function in R:

The **Repeat Function(loop) in R** executes a same block of code iteratively until a stop condition is met.

__Syntax for Repeat Function in R:____:__

The basic syntax for creating a repeat loop in R is −

if(condition) {

break

}

}

repeat loop in R, is similar to while and for loop, it will execute a block of commands repeatedly till break.

__Example__ :

__Example__

# repeat loop in R or repeat function in r sum <- 0 repeat{ sum = sum+1 print(sum) if (sum == 6){ print("repeat loop ends"); break } }

In the above example repeat loop will sum up the value until it reaches 6. Once it reaches six it prints “repeat loop ends” and breaks the loop, so the output will be

__Output:__

__Output:__

[1] 2

[1] 3

[1] 4

[1] 5

[1] 6

[1] “repeat loop ends”

__Replicate Function in R:__

__Replicate Function in R:__

**Rep() **is the function in R that replicates the values

__Syntax for Replicate Function in R:__

__Syntax for Replicate Function in R:__

- rep(value,number_of_times)
- rep(sequence,each,number_of_times)

__Example1:__

__Example1:__

# replicate values with specific number of times rep(2,10)

In the above example 2 is replicated 10 times

__output1__:

__output1__:

__Example2:__

# Replicate function in R with length attribute rep(1:4,len=20)

In the above example sequence 1 to 4 is replicated until the length reaches 20 elements

__output2__:

__output2__:

__Replicate the List in R:__

__Replicate the List in R:__

Replicate function in R is also used to replicate the list

**Example **

## replicate a list - with replicate function in R fred <- list(rating = 1:5) rep(fred, 3)

in the above example list of ratings from 1 to 5 is replicated thrice. So the output will be

[1] 1 2 3 4 5

[1] 1 2 3 4 5

[1] 1 2 3 4 5