Convert Matrix to Vector in R Last Updated : 06 Sep, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we are going to convert the given matrix into the vector in R programming language. Conversion of the matrix to vector by rowMethod 1: Using c() function Simply passing the name of the matrix will do the job. Syntax: c(matrix_name) Where matrix_name is the name of the input matrix Example 1: R # create a matrix with 12 elements # with 4 rows and 3 columns matrix=matrix(1:12,nrow=4,ncol=3) print(matrix) # convert matrix to vector using c() # function a=c(matrix) print(a) Output: Example 2: R # create a matrix with 16 elements # with 4 rows and 4 columns matrix=matrix(1:16,nrow=4,ncol=4) print(matrix) # convert matrix to vector using # c() function a=c(matrix) print(a) Output: Method 2: Using as.vector() function This function is used to convert matrix to vector so again simply passing the matrix name is enough. Syntax: as.vector(matrix) Example: R # create a matrix with 12 elements # with 4 rows and 3 columns matrix=matrix(1:12,nrow=4,ncol=3) print(matrix) # convert matrix to vector using # as.vector() function a=as.vector(matrix) print(a) Output: Example 2: R # create a matrix with 16 elements with 4 rows and 4 columns matrix=matrix(1:16,nrow=4,ncol=4) print(matrix) # convert matrix to vector using as.vector() function a=as.vector(matrix) print(a) Output Conversion of the matrix to vector by columnMethod 1: Using c() function along with t() function t() function is used to transpose the given matrix. It will transpose rows as columns and columns as rows. Syntax: t(matrix) where the matrix is the input matrix After applying t() we can apply c() and as.vector() functions to convert the matrix to vector Syntax: c(t(matrix)) Example 1: R # create a matrix with 12 elements # with 4 rows and 3 columns matrix=matrix(1:12,nrow=4,ncol=3) print(matrix) # convert matrix to vector using # c() function along with t() a=c(t(matrix)) print(a) Output: Example 2: R # create a matrix with 12 elements # with 2 rows and 6 columns matrix=matrix(1:12,nrow=2,ncol=6) print(matrix) # convert matrix to vector using # c() function along with t() a=c(t(matrix)) print(a) Output: Method 2: Using as.vector() function along with t() function The work of t() is same as above. After the transpose has been taken, the matrix is converted to vector using as.vector(). Syntax: as.vector(t(matrix)) Example R # create a matrix with 12 elements # with 2 rows and 6 columns matrix=matrix(1:12,nrow=2,ncol=6) print(matrix) # convert matrix to vector using # as.vector() function along with t() a=as.vector(t(matrix)) print(a) Output: Example 2: R # create a matrix with 4 elements # with 2 rows and 2 columns matrix=matrix(1:4,nrow=2,ncol=2) print(matrix) # convert matrix to vector using # as.vector() function along with t() a=as.vector(t(matrix)) print(a) Output Comment More infoAdvertise with us Next Article Convert Matrix to Dataframe in R G gottumukkalabobby Follow Improve Article Tags : R Language R Programs R-Matrix R-Vectors R Vector-Programs R Matrix-Programs +2 More Similar Reads Convert matrix to list in R In this article, we will discuss how to convert a given matrix to a List in R Programming Language. Conversion of a matrix into a list in Column-major order The as.list() is an inbuilt function that takes an R language object as an argument and converts the object into a list. We have used this func 2 min read How to convert matrix to list of vectors in R ? In this article, we will learn how to convert a matrix into a list of vectors in R Programming Language. Converting matrix into a list of vectors by Columns Method 1: Using as.list() function To convert columns of the matrix into a list of vectors, we first need to convert the matrix to a dataframe 4 min read Convert Named Vector to DataFrame in R In this article, we will see how to convert the named vector to Dataframe in the R Programming Language. Method 1: Generally while converting a named vector to a dataframe we may face a problem. That is, names of vectors may get converted into row names, and data may be converted into a single colu 1 min read Convert Matrix to Dataframe in R In this article, we will discuss how to convert the matrix into DataFrame, or we can also say that we will discuss how to create a DataFrame from a matrix in R Programming Language. A matrix can be converted to a dataframe by using a function called as.data.frame(). It will take each column from the 1 min read How to Convert matrix to a list of column vectors in R ? In this article, we will discuss how to convert a given matrix to a list of column vectors in R Programming Language. To do this we will take every column of the matrix and store that column in a list and at last print the list. It can be done in these ways: Using the split() functionUsing list() wi 3 min read Convert List of Vectors to DataFrame in R In this article, we will discuss how to convert the given list of vectors to Dataframe using the help of different functions in the R Programming Language. For all the methods discussed below, few functions are common because of their functionality. Let us discuss them first. as.data.frame() is one 2 min read Like