The R programming language uses data frames to store the values. Another data structure that R offers to store tabular data. It’s none other than Matrix. A matrix is a representation of a two-dimensional table with rows and columns. Just like vectors, a matrix and arrays in R can also store values. But you can see the matrix more often in mathematical computations.

Due to its high tendency for mathematical computations, you can see matrices having numerical data most of the time.

## Creating a Matrix in R

ToÂ create a matrix in R, you have to pass a vector as an input to the matrix() function. Along with this, you need to specify the parameters such as the number of rows and columns.Â

If you are intended to create a 2 x 2 matrix, you need to specify the rows = 2 to divide the data into 2 rows. Let’s see how it works.

**Without parameters:**

```
#passing a vector into matrix
df <- matrix(c(2,4,6,8))
#display matrix
df
```

```
[,1]
[1,] 2
[2,] 4
[3,] 6
[4,] 8
```

This is an instance of the matrix creation without specifying any parameters. Hence, the matrix() function returned a single column matrix.

Now, let’s see how we can specify the nrows and ncols parameters in matrix creation in R programming.

```
#Constructs the matrix with 2 rows
df<- matrix(c(2,4,6,8),nrow = 2)
#display the matrix
df
```

```
[,1] [,2]
[1,] 2 6
[2,] 4 8
```

Also note that, this is also equivalent to the **ncols = 2**. I will also show you that with this example.

```
#Constructs the matrix with 2 rows
df<- matrix(c(2,4,6,8),ncol = 2)
#display the matrix
df
```

```
[,1] [,2]
[1,] 2 6
[2,] 4 8
```

Did you observe that R is loading the first column before the second? This process is called **Column major process.** This is the default method of loading matrix in R.

*“You can also overturn this settings by specifying the parameter as byrow=TRUE”*

## Adding more values to a Matrix in R

Let’s see what happens if we add more values to our matrix. Start with 6 values instead of 4.

In this, we have passed a vector as input with 6 values and without any parameters. Hence the matrix function has returned the single-column matrix as shown here.

```
#Simple matrix in R
df<- matrix(c(1,2,3,4,5,6))
#display matrix
df
```

```
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
```

Now, let’s make use of parameters and we can try specifying nrows = 2. The matrix function returned a 2×3 matrix.

```
#Matrix with row count
df<- matrix(c(1,2,3,4,5,6),nrow = 2)
#display matrix
df
```

```
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
```

As I have said earlier, matrices in R will work as a column-major method by default, and here it is. It has first loaded the first column and then the second. The difference here is the dimensions.

If you specify the row count, then you will get a 2×3 matrix and if you mention column count you will get a 3×2 matrix.

```
#matrix with column count
df<- matrix(c(1,2,3,4,5,6),ncol = 2)
#display matrix
df
```

```
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
```

Finally, we have arrived here. As I told you above, you can change the default settings of **column-major methods** by specifying **byrow = TRUE**. Let’s see how it works.

```
#matrix with byrow parameter
df<- matrix(c(1,2,3,4,5,6),ncol = 2,byrow = T)
#display matrix
df
```

```
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
```

Fantastic! You have successfully overturned the settings using **byrow=TRUE **argument.

## Accessing elements from Matrices

Just like data frames, the elements present in the matrix can also be extractable or accessible.

Let’s see how we can extract single elements and entire rows and columns as well. We will be using the same matrix created above.

```
#Matrix with row count
df<- matrix(c(1,2,3,4,5,6),nrow = 2)
#display matrix
df
```

```
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
```

Our matrix is ready to be extracted. Let’s try to extract first and third values from it.

```
df[1,1]
df[1,3]
```

```
1
5
```

The matrix will be in the form of m rows and n columns. So we have extracted the first and third elements of the matrix with ease. Similarly, you can access any element in a matrix by specifying the location.

Let’s extract the entire row and column.

```
df[1,]
df[2,]
```

```
1 3 5
2 4 6
```

```
df[,1]
df[,2]
df[,3]
```

```
1 2
3 4
5 6
```

Well, it always feels good to see the results as we expected, and here it is. The first 3 lines will return the 2 rows and entire columns. Another three lines will return entire rows and 1 column. That’s the **beauty of Matrix in R.**

## Arrays in R programming

Arrays are data objects that can store data of multiple dimensions. In contrast to a matrix, arrays will store only one data type.

Let’s create a simple array in R with the dim parameter. The dim parameter takes the following argument: `dim(row, column, number_of_arrays)`

```
#creating 2 vectors
df1<- c(1,2,3)
df2<-c(2,4,6,8,10)
#Creating an array with dim parameter
df<- array(c(df1,df2),dim = c(3,3,2))
#display array
df
```

```
, , 1
[,1] [,2] [,3]
[1,] 1 2 8
[2,] 2 4 10
[3,] 3 6 1
, , 2
[,1] [,2] [,3]
[1,] 2 4 10
[2,] 3 6 1
[3,] 2 8 2
```

This is a simple array in R. Upon mentioning the dimensions, the array function will create an array with the input data i.e. vectors.

## Accessing the array elements in R

Consider the array that we created above. Let’s try to extract the elements present in the array just like we did with the matrix in the earlier sections.

We have 2 matrices as shown above. Let’s print or extract the 1st row of the first array. These are our 2 arrays for your reference.

```
, , 1
[,1] [,2] [,3]
[1,] 1 2 8
[2,] 2 4 10
[3,] 3 6 1
, , 2
[,1] [,2] [,3]
[1,] 2 4 10
[2,] 3 6 1
[3,] 2 8 2
```

```
#Return first row of first array
df[1,,1]
```

```
[1] 1 2 8
```

Whoops! We got the first row of the first array. Let’s dig deep and extract both rows and columns of an array.

```
#return element present in first row, second column
df[1,2,2]
```

```
4
```

Great! we have printed the element present in the first row, the second column of the second array i.e. 4. It’s time to access the whole array!

```
#Return first array
df[,,1]
```

```
[,1] [,2] [,3]
[1,] 1 2 8
[2,] 2 4 10
[3,] 3 6 1
```

That’s it. This is all about accessing the elements of an array. Hope you enjoyed it.

## Let’s do some calculations using across arrays

To perform any calculation across the arrays in R, you need to use the function,** apply()**.

**The Syntax:**

```
apply(x,margin,fun)
```

**Where,**

**x =**The input data / array**Margin =**name of dataset**fun =**Function to be applied across data

Let’s use the arrays that we have created earlier. Now, we are going to perform some arithmetical computations across these arrays. Go, Go, Go!

```
, , 1
[,1] [,2] [,3]
[1,] 1 2 8
[2,] 2 4 10
[3,] 3 6 1
, , 2
[,1] [,2] [,3]
[1,] 2 4 10
[2,] 3 6 1
[3,] 2 8 2
```

```
#Returs the sum of first rows of all arrays
x <- apply(df, c(1), sum)
#display result
x
```

```
27 26 22
```

Like this, you can use any function and you can add, subtract, multiply, divide, and much more. You are free to do any computations using **apply() **functions.

## Final words

A matrix in R is a data structure used to store tabular data and mathematical computations as well. It stores the values in rows and columns fashion.

All you need to create a matrix is to pass a vector as input with the row and column count mentioned. And just like that, your matrix will be ready. You can access the elements as shown above using the data point location.

The matrix and arrays in R are the key data structures and objects which will assist you in many things.

Arrays are the data objects which will deal with multi-dimensional data. You can compute, access, and create arrays using the array() function as shown here.

That’s all for now. **Happy Matrix and Arrays in R!!!**

**More read: **R documentation