Hello, readers! In this article, we will be focusing on exploring 2 Easy Ways to convert a Dataframe to a matrix in R programming, in detail.

So, let us begin!

## 1. R data.matrix() function to convert a dataframe to a matrix

R provides us with a built-in function to perform the conversion of a data frame to a matrix. The `data.matrix()`

function enables us to perform the conversion of a data frame to a matrix in R.

With data.matrix() function, we manipulate the structure of the data frame to fit in the schema of a matrix.

That is, as discussed above, data frame deals with multiple data types in a multi column structure, while matrix solely occupies data with fixed rows and columns in a rectangular format.

To comply with this schema, the data.matrix() function internally converts the multi column structure into a rectangular matrix format and converts the character or factor type data to numeric form.

**Syntax:**

```
data.matrix(dataframe)
```

The data.frame() function converts all the data values of the data frame into a numeric format and then it binds all the values to form a rectangular matrix structure.

**Example:**

```
rm(list = ls())
data = data.frame(
"Name" = c("Jose", "Chris", "Akash"),
"City" = c("Rajasthan", "Texas", "California")
)
print(data)
mat <- data.matrix(data)
print(mat)
```

**Output:**

As seen below, prior to applying the function, the data frame happens to have factor data. After applying the data.matrix() function, the data frame gets converted into a matrix format with all the factor values being altered to numeric form.

```
> print(data)
Name City
1 Jose Rajasthan
2 Chris Texas
3 Akash California
>
> mat <- data.matrix(data)
> print(mat)
Name City
[1,] 3 2
[2,] 2 3
[3,] 1 1
```

## 2. R as.matrix() function to convert data frame to matrix

With as.matrix() function, we can convert any R object or data structure to a matrix type. By this, we mean to say that a list of values or even a data frame can be manipulated to matrix type.

Unlike data.matrix() function, R `as.matrix()`

function does not manipulate the factor values of the data frame to numeric while conversion. It retains the original data type of the columns. Just the structure and format of the objects gets converted to a matrix form.

**Example 1:**

In this example, we have converted a vector into a matrix form.

```
rm(list = ls())
data <- c(1:10)
as.matrix(data)
```

**Output:**

```
> as.matrix(data)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
```

**Example 2:**

In this example, we have converted a data frame to a matrix format. Unlike the data.matrix() function, here we do not witness any manipulation of factor values to numeric format. Only the structure of the data frame gets converted to a matrix form.

```
rm(list = ls())
data = data.frame(
"Name" = c("Jose", "Chris", "Akash"),
"City" = c("Rajasthan", "Texas", "California")
)
print(data)
mat <- as.matrix(data)
print(mat)
```

**Output:**

```
> print(data)
Name City
1 Jose Rajasthan
2 Chris Texas
3 Akash California
>
> print(mat)
Name City
[1,] "Jose" "Rajasthan"
[2,] "Chris" "Texas"
[3,] "Akash" "California"
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned!

Till then, Happy Learning!! 馃檪