Examples for 'base::rep'


Replicate Elements of Vectors and Lists

Aliases: rep rep.factor rep.int rep.POSIXct rep.POSIXlt rep.Date rep_len

Keywords: manip chron

### ** Examples

rep(1:4, 2)
[1] 1 2 3 4 1 2 3 4
rep(1:4, each = 2)       # not the same.
[1] 1 1 2 2 3 3 4 4
rep(1:4, c(2,2,2,2))     # same as second.
[1] 1 1 2 2 3 3 4 4
rep(1:4, c(2,1,2,1))
[1] 1 1 2 3 3 4
rep(1:4, each = 2, length.out = 4)    # first 4 only.
[1] 1 1 2 2
rep(1:4, each = 2, length.out = 10)   # 8 integers plus two recycled 1's.
 [1] 1 1 2 2 3 3 4 4 1 1
rep(1:4, each = 2, times = 3)         # length 24, 3 complete replications
 [1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4
rep(1, 40*(1-.8)) # length 7 on most platforms
[1] 1 1 1 1 1 1 1
rep(1, 40*(1-.8)+1e-7) # better
[1] 1 1 1 1 1 1 1 1
## replicate a list
fred <- list(happy = 1:10, name = "squash")
rep(fred, 5)
$happy
 [1]  1  2  3  4  5  6  7  8  9 10

$name
[1] "squash"

$happy
 [1]  1  2  3  4  5  6  7  8  9 10

$name
[1] "squash"

$happy
 [1]  1  2  3  4  5  6  7  8  9 10

$name
[1] "squash"

$happy
 [1]  1  2  3  4  5  6  7  8  9 10

$name
[1] "squash"

$happy
 [1]  1  2  3  4  5  6  7  8  9 10

$name
[1] "squash"
# date-time objects
x <- .leap.seconds[1:3]
rep(x, 2)
[1] "1972-07-01 GMT" "1973-01-01 GMT" "1974-01-01 GMT" "1972-07-01 GMT"
[5] "1973-01-01 GMT" "1974-01-01 GMT"
rep(as.POSIXlt(x), rep(2, 3))
[1] "1972-07-01 GMT" "1972-07-01 GMT" "1973-01-01 GMT" "1973-01-01 GMT"
[5] "1974-01-01 GMT" "1974-01-01 GMT"
## named factor
x <- factor(LETTERS[1:4]); names(x) <- letters[1:4]
x
a b c d 
A B C D 
Levels: A B C D
rep(x, 2)
a b c d a b c d 
A B C D A B C D 
Levels: A B C D
rep(x, each = 2)
a a b b c c d d 
A A B B C C D D 
Levels: A B C D
rep.int(x, 2)  # no names
a b c d a b c d 
A B C D A B C D 
Levels: A B C D
rep_len(x, 10)
a b c d a b c d a b 
A B C D A B C D A B 
Levels: A B C D

[Package base version 4.2.3 Index]