R How to Convert a Matrix to a One-Dimensional Array (Example Code)

This tutorial shows how to convert a matrix to a vector object in the R programming language.

Creation of Example Data

data(iris)                     # Load and prepare example data
iris_mat <- as.matrix(iris[ , 1:4])
head(iris_mat)                 # Return head of example data
#      Sepal.Length Sepal.Width Petal.Length Petal.Width
# [1,]          5.1         3.5          1.4         0.2
# [2,]          4.9         3.0          1.4         0.2
# [3,]          4.7         3.2          1.3         0.2
# [4,]          4.6         3.1          1.5         0.2
# [5,]          5.0         3.6          1.4         0.2
# [6,]          5.4         3.9          1.7         0.4

Example: Applying c() Function to Convert Matrix to One-Dimensional Vector

iris_vec <- c(iris_mat)        # Using c function in R
iris_vec                       # Returning vector output
# 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 ...

Related Tutorials & Further Resources

You may find some related R tutorials on topics such as data conversion, lists, indices, and matrices below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top