# Repeat and Replicate function in R

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 −

repeat {
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 :
```# 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:
 1
 2
 3
 4
 5
 6
 “repeat loop ends”

### Replicate Function in R:

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

#### Syntax for Replicate Function in R:

1. rep(value,number_of_times)
2. rep(sequence,each,number_of_times)
##### Example1:
```# replicate values with specific number of times

rep(2,10)
```

In the above example 2 is replicated 10 times

##### output1:
 2 2 2 2 2 2 2 2 2 2
##### 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:
 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

#### 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

\$rating
 1 2 3 4 5
\$rating
 1 2 3 4 5
\$rating
 1 2 3 4 5