Friday, September 8, 2017

cbind() rbind() {base}


rbind() and cbind() functions allows to combine sequences by rows or columns respectively.

The arguments are:
  • vectors or matrices the be combined.
  • deparse.level: integer controlling the construction of labels in the case of non-matrix-like arguments (for the default method): deparse.level = 0 constructs no labels; the default, deparse.level = 1 or 2 constructs labels from the argument names.
cbind(), bind by columns:
dim(iris)
## [1] 150   5
head = head(iris)
head
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
dim(head)
## [1] 6 5
tail = tail(iris)
tail
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
dim(tail)
## [1] 6 5
bindcolumns = cbind(head, tail)
bindcolumns
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length
## 1          5.1         3.5          1.4         0.2  setosa          6.7
## 2          4.9         3.0          1.4         0.2  setosa          6.7
## 3          4.7         3.2          1.3         0.2  setosa          6.3
## 4          4.6         3.1          1.5         0.2  setosa          6.5
## 5          5.0         3.6          1.4         0.2  setosa          6.2
## 6          5.4         3.9          1.7         0.4  setosa          5.9
##   Sepal.Width Petal.Length Petal.Width   Species
## 1         3.3          5.7         2.5 virginica
## 2         3.0          5.2         2.3 virginica
## 3         2.5          5.0         1.9 virginica
## 4         3.0          5.2         2.0 virginica
## 5         3.4          5.4         2.3 virginica
## 6         3.0          5.1         1.8 virginica
dim(bindcolumns)
## [1]  6 10

rbind(), bind by rows:
dim(iris)
## [1] 150   5
head = head(iris)
head
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
dim(head)
## [1] 6 5
tail = tail(iris)
tail
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
dim(tail)
## [1] 6 5
bindrows = rbind(head, tail)
bindrows
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 1            5.1         3.5          1.4         0.2    setosa
## 2            4.9         3.0          1.4         0.2    setosa
## 3            4.7         3.2          1.3         0.2    setosa
## 4            4.6         3.1          1.5         0.2    setosa
## 5            5.0         3.6          1.4         0.2    setosa
## 6            5.4         3.9          1.7         0.4    setosa
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
dim(bindrows)
## [1] 12  5

deparse.level parameter has 3 options: 0,1 and 2. Depending of the options columns name’s will be different as showed in the following piece of code. Default value is 1.
m = 1:5
m
## [1] 1 2 3 4 5
cbind(m,1:10) #default value is 1
##       m   
##  [1,] 1  1
##  [2,] 2  2
##  [3,] 3  3
##  [4,] 4  4
##  [5,] 5  5
##  [6,] 1  6
##  [7,] 2  7
##  [8,] 3  8
##  [9,] 4  9
## [10,] 5 10
cbind(m,1:10, deparse.level = 0)
##       [,1] [,2]
##  [1,]    1    1
##  [2,]    2    2
##  [3,]    3    3
##  [4,]    4    4
##  [5,]    5    5
##  [6,]    1    6
##  [7,]    2    7
##  [8,]    3    8
##  [9,]    4    9
## [10,]    5   10
cbind(m,1:10, deparse.level = 1)
##       m   
##  [1,] 1  1
##  [2,] 2  2
##  [3,] 3  3
##  [4,] 4  4
##  [5,] 5  5
##  [6,] 1  6
##  [7,] 2  7
##  [8,] 3  8
##  [9,] 4  9
## [10,] 5 10
cbind(m,1:10, deparse.level = 2)
##       m 1:10
##  [1,] 1    1
##  [2,] 2    2
##  [3,] 3    3
##  [4,] 4    4
##  [5,] 5    5
##  [6,] 1    6
##  [7,] 2    7
##  [8,] 3    8
##  [9,] 4    9
## [10,] 5   10

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:   ...