R Program to convert a given matrix to a one dimensional

February 7, 2023, Learn eTutorial

How to convert a given matrix to a one-dimensional one using R programming?

Here we are explaining how to write an R program to convert a given matrix to a one-dimensional matrix. For that, we are using a built-in function as.vector() for this conversion. This method helps to converts a distributed matrix into a non-distributed vector. Also, this method helps to convert any object into a vector.

as.vector(obj, mode = "any", proc.dest = "all")


  • obj: Any obj and returns the vector or tries to coerce the obj into a vector of mode.
  • mode: Character string giving an atomic mode or “list“, or “any”.
  • proc.dest: Destination process for storing the matrix.

In this R program, we are printing the original matrix first and then we directly give the values to built-in functions. And print the function result. Here we used variables mtx, and x for assigning vector values and matrix.


STEP 1: Assign variable mtx with matrix values

STEP 2: First print the original matrix values

STEP 3: Convert the matrix using as.vector()

STEP 4: Assign the result array into variable x

STEP 5: print the result of the function as one-dimensional array


R Source Code

print("Original matrix is:")
x = as.vector(mtx)
print("1 dimensional array is:")



[1] "Original matrix is"
[,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
[1] "1 dimensional array is:"
 [1]  1  2  3  4  5  6  7  8  9 10 11 12