Sequences and repetitions

R basics
Numbers
Author

Karen C. Goncalves dos Santos

Published

October 3, 2023

seq() and rep fucntions

seq

Use the command seq to get a sequence of numbers. You can specify the interval between every number shown. The function is writen as: seq(FROM, TO, INTERVAL)

seq(1, 2, 0.1)
 [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0

If you want to see every integer between two numbers you can use : or you can omit the interval in the seq command.

# Get very integer between 0 and 10
seq(0, 10)
 [1]  0  1  2  3  4  5  6  7  8  9 10
0:10
 [1]  0  1  2  3  4  5  6  7  8  9 10
# Get every integer between 0 and 10 in decreasing order
seq(10, 0)
 [1] 10  9  8  7  6  5  4  3  2  1  0
10:0
 [1] 10  9  8  7  6  5  4  3  2  1  0

rep

Syntax: rep(X, TIMES, EACH)

  • X - what you want to repeat: vector of characters, numbers or logical (T/F)
  • TIMES: repeat how many times
  • EACH: if there is more than one element in WHAT, you repeat the first, then the second…
# repeat X (1 in this case) 4 times
rep(1, 4)
[1] 1 1 1 1
# repeat X (vector c(1, 2)) 4 times
rep(c(1, 2), 4)
[1] 1 2 1 2 1 2 1 2
# repeat the elements of the input 4 times each
rep(c(1, 2), each = 4)
[1] 1 1 1 1 2 2 2 2
Back to top