**rep** and **seq** are basic functions in R that are very powerful for a wide variety of tasks in R. **rep** repeats characters and seq creates a sequence of characters.

The **rep** function has 2 parameters, the value to repeat and the number of times to repeat it. Here’s some examples of the **rep** function:

Repeat the number 1 five times.

`rep(1, 5)`

`## [1] 1 1 1 1 1`

Repeat the sequence of values 1-5, three times.

`rep(1:5, 3)`

`## [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5`

Repeat the days of the week twice.

```
days=c("mon","tue","wed","thu","fri","sat","sun")
rep(days, 2)
```

```
## [1] "mon" "tue" "wed" "thu" "fri" "sat" "sun" "mon" "tue" "wed" "thu"
## [12] "fri" "sat" "sun"
```

Now, let’s look at the **seq** function. There are three parameters, the start of the sequence, the end, and the interval in that order.

Here’s a sequence from 1 to 5:

`seq(from = 1, to = 5, by = 1)`

`## [1] 1 2 3 4 5`

Here’s a sequence from 0 to 10 by 2:

`seq(from = 0, to = 10, by = 2)`

`## [1] 0 2 4 6 8 10`

Finally, let’s merge this newly created data together using the function **cbind**. ** cbind** stands for column bind, and will merge vectors together to form a matrix.

In this example we create two vectors of data (x & y) using the concatenate function, then we will merge them using the **cbind** function:

```
x = c(1:5)
y = c(-1:-5)
cbind(x, y)
```

```
## x y
## [1,] 1 -1
## [2,] 2 -2
## [3,] 3 -3
## [4,] 4 -4
## [5,] 5 -5
```

Finally, let’s add more data to the matrix we created above.

```
x = c(1:5)
y = c(-1:-5)
data = cbind(x, y)
a = c(1,1,1,1,1)
b = c(2,2,2,2,2)
cbind(data, a, b)
```

```
## x y a b
## [1,] 1 -1 1 2
## [2,] 2 -2 1 2
## [3,] 3 -3 1 2
## [4,] 4 -4 1 2
## [5,] 5 -5 1 2
```

For more information on the **cbind** function, check out my other post here.

## 3 thoughts on “rep, seq, and cbind: Data Creation and Processing”