Tuesday, October 3, 2017

seq() {base}


seq() is a functions that generates regular sequences.
 seq.int() is a primitive which can be much faster but has a few restrictions.
 seq_along() and seq_len() are very fast primitives for two common cases.

seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),length.out = NULL, along.with = NULL, ...)
seq.int(from, to, by, length.out, along.with)
seq_along(along.with)
seq_len(length.out)

The parameters are:
 - from,to: starting and end values of the sequence
 - by: increment of the sequence
 - lenght.out: length of the sequence
 - along.with: take the length from the length of this argument

seq():
seq(10)
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(1,10)
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(from = 1, to = 15, by = 2)   #from 1 to 15 every second number
## [1]  1  3  5  7  9 11 13 15
seq(from = 1, to = 15, by = 3)   #from 1 to 15 every third  number
## [1]  1  4  7 10 13
seq(from = 1, to = 10, length.out = 5)  #from 1 to 10, with a total sequence length of 5 
## [1]  1.00  3.25  5.50  7.75 10.00
seq(from = 1, to = 10, length.out = 10) #from 1 to 10, with a total sequence length of 5
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(from = 1, by = 2, length.out = 5) # creates a seq from 1 and every second number, with a total lenght 5 
## [1] 1 3 5 7 9
seq(from = 10, by = 3, length.out = 10) # creates a seq from 10 and every third number, with a total lenght 10 
##  [1] 10 13 16 19 22 25 28 31 34 37
#seq(from = 1, to = 15, by = 2,  length.out = 5)   --Error, too many arguments!
seq(along.with = c(1:10))
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(along.with = c(11:20))
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(11,20, along.with = c(1:10))
##  [1] 11 12 13 14 15 16 17 18 19 20
seq(11,20, along.with = c(1:10))
##  [1] 11 12 13 14 15 16 17 18 19 20

seq.int():
seq(11,20)
##  [1] 11 12 13 14 15 16 17 18 19 20
seq.int(11,20)    #same result
##  [1] 11 12 13 14 15 16 17 18 19 20
y = 12:34
y
##  [1] 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#seq(y, length.out = 6)    -- Error, 'from' must be of length 1!
seq(12,34, length.out = 6)
## [1] 12.0 16.4 20.8 25.2 29.6 34.0
seq.int(y, length.out = 6) #different result
## [1] 12 13 14 15 16 17

seq_along():
seq_along() only uses one argument.
x = 1:4 ; y = 5:10 ; z = 10
x
## [1] 1 2 3 4
y
## [1]  5  6  7  8  9 10
z
## [1] 10
seq(x)
## [1] 1 2 3 4
seq(y)
## [1] 1 2 3 4 5 6
seq(z)
##  [1]  1  2  3  4  5  6  7  8  9 10
seq_along(x)
## [1] 1 2 3 4
seq_along(y)
## [1] 1 2 3 4 5 6
seq_along(z)
## [1] 1
#seq_along(1,20) --Error, 2 arguments passed to 'seq_along' which requires 1

seq_len():
seq_len() only uses one argument.
x
## [1] 1 2 3 4
y
## [1]  5  6  7  8  9 10
z
## [1] 10
seq(x)
## [1] 1 2 3 4
seq(y)
## [1] 1 2 3 4 5 6
seq(z)
##  [1]  1  2  3  4  5  6  7  8  9 10
seq_len(x) #seq_len only uses the first argument
## Warning in seq_len(x): first element used of 'length.out' argument
## [1] 1
seq_len(y) #seq_len only uses the first argument
## Warning in seq_len(y): first element used of 'length.out' argument
## [1] 1 2 3 4 5
seq_len(z) 
##  [1]  1  2  3  4  5  6  7  8  9 10
par(mfrow = c(1,3))
plot(seq(from = 1, to = 15, by = 2), type = 'b', col = 'brown')
plot(seq(from = 1, to = 15, length.out = 5), type = 'b', col = 'deeppink')
plot(seq(from = 1, by = 2, length.out = 6), type = 'b', col = 'orange')

No comments:

Post a Comment

duplicated() {base}

duplicated()  function determines which elements are duplicated and returns a logical vector. The parameters of the function are:   ...