R rep Function


rep() function replicates the values in x.

rep(x, ...)
rep.int(x, times)

x: numeric vector
...: arguments including times (default = 1), length.out, each (each elements how many times)


>x <- rep(1:5)
[1] 1 2 3 4 5

Repeat 1 -5 two times:
>x <- rep(1:5,2)
 [1] 1 2 3 4 5 1 2 3 4 5

Convert to a 5 × 2 matrix:
>dim(x) <- c(5,2)
>x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    2    4
[2,]    2    4    1    3    5

Each element replicates two times:
 x <- rep(1:5,each=2)
 [1] 1 1 2 2 3 3 4 4 5 5

Convert to a 5 × 2 matrix:
>dim(x) <- c(5,2)
>x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    1    2    3    4    5

> rep.int(1:5,2)
 [1] 1 2 3 4 5 1 2 3 4 5