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')

Monday, October 2, 2017

rep() {base}


rep() function replicates the values in x. It is a generic function.

rep.int and rep_len are faster simplified versions for two common cases. They are not generic.

rep(x, ...)
rep.int(x, times)
rep_len(x, length.out)

The parameters are:
  • x : vector or factor to be repeated.
  • times: an integer valued vector giving the times to repeat each element.
  • length.out: length of the output
  • each: the times each element in x is repeated

rep():
rep(1:3, times = 2) #from 1 to 3, 2 times.
## [1] 1 2 3 1 2 3
rep(1:3, 2)         #from 1 to 3, 2 times.
## [1] 1 2 3 1 2 3
rep(1:3, each = 2)  #from 1 to 3, each value 2 times    
## [1] 1 1 2 2 3 3
rep(1:3, c(2,2,2))  #time of each value from 1 to 3 is given by a vector
## [1] 1 1 2 2 3 3
rep(1:3, c(0,1,2))
## [1] 2 3 3
rep(1:3, each = 3, len = 3)    # 3 first values
## [1] 1 1 1
rep(1:3, each = 3, len = 12)   # 12 integers, 3 numbers are recycled
##  [1] 1 1 1 2 2 2 3 3 3 1 1 1
rep(1:3, each = 3, times = 3) 
##  [1] 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3

rep.int():
x = data.frame(1:10,21:30)
colnames(x) = c('A','B') 
x
##     A  B
## 1   1 21
## 2   2 22
## 3   3 23
## 4   4 24
## 5   5 25
## 6   6 26
## 7   7 27
## 8   8 28
## 9   9 29
## 10 10 30
rep(x, times = 2) 
## $A
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $B
##  [1] 21 22 23 24 25 26 27 28 29 30
## 
## $A
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $B
##  [1] 21 22 23 24 25 26 27 28 29 30
rep.int(x, times = 2)
## [[1]]
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## [[2]]
##  [1] 21 22 23 24 25 26 27 28 29 30
## 
## [[3]]
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## [[4]]
##  [1] 21 22 23 24 25 26 27 28 29 30

rep_len():
rep(1,6)
## [1] 1 1 1 1 1 1
rep_len(1,6) #same result
## [1] 1 1 1 1 1 1
rep(1:5, 10) #number of times = 10
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
## [36] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
rep_len(1:5, 10) #length = 10
##  [1] 1 2 3 4 5 1 2 3 4 5
par(mfrow = c(2,2))
y = c(1,2,3)
plot(y, type = 'b', col = 'gold')
plot(rep(y, time = 2), type = 'b', col = 'darkturquoise')
plot(rep(y, time = 2, each = 2), type = 'b', col = 'darkviolet')
plot(rep(y, time = 2, each = 2, len = 6), type = 'b', col = 'deeppink')

duplicated() {base}

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