Sunday 21 June 2015

R : cbind and rbind

cbind()
cbind() function combines vector, matrix or data frame by columns.
Syntax
cbind(x1,x2,...)

> matrix1 <- matrix(1:6, nrow=3, ncol=2)
> matrix1
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
> 
> matrix2 <- matrix(7:12, nrow=3, ncol=2)
> matrix2
     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12
> 
> cbind(matrix1, matrix2)
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

rbind
rbind() function combines vector, matrix or data frame by rows.

Syntax

rbind(x1,x2,...)
> rbind(matrix1, matrix2)
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
[4,]    7   10
[5,]    8   11
[6,]    9   12


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment