In R programming language, **vector** is a basic object which consists of homogeneous elements. The data type of vector can be integer, double, character, logical, complex or raw. A vector can be created by using ** c()** function.

**Syntax:**

x <- c(val1, val2, .....)

Vectors in R are the same as the arrays in C language which are used to hold multiple data values of the same type. Vectors can also be used to create matrices.

Matrices can be created with the help of Vectors by using pre-defined functions in R Programming Language. These functions take vectors as arguments along with several other arguments for matrix dimensions, etc.

Functions used for Matrix creation:

**matrix()**function**cbind()**function**rbind()**function

`matrix()`

function

The available data is present in a single/multiple vector, then **matrix()** function can be used to create the matrix by passing the following arguments in the function.

**Syntax:**

matrix(data, nrow, ncol, byrow, dimnames)

**where, **

datais the input vector which represents the elements in the matrix

nrowspecifies the number of rows to be created

ncolspecifies the number of columns to be created

byrowspecifies logical value. If TRUE, matrix will be filled by row. Default value is FALSE.

dimnamesspecifies the names of rows and columns

**Example 1:**

`# defining data in the vector ` `x <` `-` `c(` `5` `:` `16` `) ` ` ` `# defining row names and column names ` `rown <` `-` `c(` `"row_1"` `, ` `"row_2"` `, ` `"row_3"` `) ` `coln <` `-` `c(` `"col_1"` `, ` `"col_2"` `, ` `"col_3"` `, ` `"col_4"` `) ` ` ` `# creating matrix ` `m <` `-` `matrix(x, nrow ` `=` `3` `, byrow ` `=` `TRUE, ` ` ` `dimnames ` `=` `list` `(rown, coln)) ` ` ` `# print matrix ` `print` `(m) ` ` ` `# print class of m ` `class` `(m) ` |

*chevron_right*

*filter_none*

**Output:**

col_1 col_2 col_3 col_4 row_1 5 6 7 8 row_2 9 10 11 12 row_3 13 14 15 16 [1] "matrix"

If matrix to be created using data present in multiple vectors, then vector of multiple vectors is created and passed to ** matrix()** function as an argument.

**Example 2:**

`# defining multiple vectors ` `x <` `-` `c(` `1` `:` `5` `) ` `y <` `-` `c(` `11` `:` `15` `) ` `z <` `-` `c(` `21` `:` `25` `) ` ` ` `# creating matrix ` `m <` `-` `matrix(c(x, y, z), ncol ` `=` `3` `) ` ` ` `# print matrix ` `print` `(m) ` ` ` `# print class of m ` `class` `(m) ` |

*chevron_right*

*filter_none*

**Output:**

[, 1] [, 2] [, 3] [1, ] 1 11 21 [2, ] 2 12 22 [3, ] 3 13 23 [4, ] 4 14 24 [5, ] 5 15 25 [1] "matrix"

`cbind()`

function

** cbind()** function in R programming is used to combine vectors, data frames or matrices by columns and number of rows of data sets should be equal otherwise, output will be incomprehensible.

**Syntax:**

cbind(v1, v2, v3, ....., deparse.level)

**where, **

v1, v2, v3, ….represent vectors, matrices or data frames

deparse.levelused for constructing labels for non-matrix like arguments. 0 for no labels. Default value is 1 or 2 for labelling from the arguments names.

**Example:**

`# defining multiple vectors ` `x <` `-` `c(` `1` `:` `5` `) ` `y <` `-` `c(` `11` `:` `15` `) ` `z <` `-` `c(` `21` `:` `25` `) ` ` ` `# creating matrix ` `m <` `-` `cbind(x, y, z) ` ` ` `# print matrix ` `print` `(m) ` ` ` `# print class of m ` `class` `(m) ` |

*chevron_right*

*filter_none*

**Output:**

x y z [1, ] 1 11 21 [2, ] 2 12 22 [3, ] 3 13 23 [4, ] 4 14 24 [5, ] 5 15 25 [1] "matrix"

`rbind()`

function

** rbind()** function in R programming is used to combine vectors, data frames or matrices by rows and number of columns of data sets should be equal otherwise, output will be insignificant.

**Syntax:**

rbind(v1, v2, v3, ....., deparse.level)

**where, **

v1, v2, v3, ….represent vectors, matrices or data frames

deparse.levelused for constructing labels for non-matrix like arguments. 0 for no labels. Default value is 1 or 2 for labelling from the arguments names.

**Example:**

`# defining multiple vectors ` `x <` `-` `c(` `1` `:` `5` `) ` `y <` `-` `c(` `11` `:` `15` `) ` `z <` `-` `c(` `21` `:` `25` `) ` ` ` `# creating matrix ` `n <` `-` `rbind(x, y, z) ` ` ` `# print matrix ` `print` `(n) ` ` ` `# print class of m ` `class` `(n) ` |

*chevron_right*

*filter_none*

**Output:**

[, 1] [, 2] [, 3] [, 4] [, 5] x 1 2 3 4 5 y 11 12 13 14 15 z 21 22 23 24 25 [1] "matrix"

## Recommended Posts:

- Create a Data Frame of all the Combinations of Vectors passed as Argument in R Programming - expand.grid() Function
- Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
- Combine Vectors, Matrix or Data Frames by Rows in R Language - rbind() Function
- Operations on Vectors in R
- List of Vectors in R
- Getting and Setting Length of the Vectors in R Programming - length() Function
- Compute the beta value of Non-Negative Numeric Vectors in R Programming - beta() Function
- Get the natural logarithm of the beta value of Non-Negative numeric vectors in R Language - lbeta() Function
- Compute the Covariance between Two Vectors in R Programming - cov() Function
- Compute the Correlation Coefficient Value between Two Vectors in R Programming - cor() Function
- Creating a Data Frame from Vectors in R Programming
- Generating sequenced Vectors in R Programming - sequence() Function
- Find positions of Matching Elements between Vectors in R Programming - match() Function
- Generate Color Vectors of desired Length in R Programming - rainbow() Function
- Compute the Parallel Minima and Maxima between Vectors in R Programming - pmin() and pmax() Functions
- Assigning Vectors in R Programming
- Types of Vectors in R Programming
- Append Operation on Vectors in R Programming
- Dot Product of Vectors in R Programming
- Cross Product of Vectors in R Programming

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.