Vector is a fundamental data structure that contains elements of the same type. To check the data type of variable in R, use the typeof() function. To create a vector, use the c() function.
as.vector() in R
The as.vector() is a built-in R method that converts a distributed matrix into a non-distributed vector. The as.vector() is a generic method that attempts to coerce its argument into a mode vector.
To convert any object to a vector in R, use the as.vector() method.
Syntax
as.vector(obj, mode = "any", proc.dest = "all")
Parameters
obj: The as.vector() function takes any obj and returns the vector or tries to coerce the obj into a vector of mode.
mode: It is a character string giving an atomic mode or “list“, or (except for ‘vector’) “any”.
proc.dest: It is a destination process for storing the matrix.
Return Value
It returns an ordinary vector from the R object.
Example
Define an array that is an R object and convert that array to vector using as.vector() method.
arr <- array(c(12, 13, 14, 17, 21, 51), c(3, 2))
arr
class(arr)
vt <- as.vector(arr)
vt
class(vt)
Output
[,1] [,2]
[1,] 12 17
[2,] 13 21
[3,] 14 51
[1] "matrix" "array"
[1] 12 13 14 17 21 51
[1] "numeric"
As you can see that we have converted an array object to a vector using as.vector() function.
Convert Matrix into Vector using as.vector()
To convert a matrix into a vector, use the as.vector() function. The as.vector() function produces a vector of the given length and mode. If we want to create a vector of consecutive numbers, the : operator is beneficial.
mtrx <- matrix(c(1:16), 4, 4)
mtrx
class(mtrx)
vt <- as.vector(mtrx)
vt
class(vt)
Output
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
[1] "matrix" "array"
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[1] "integer"
And we get the ordinary vector from an array in R. For as.vector, a vector. All attributes are removed from the result of an atomic mode, but not in general for a list result. The default method handles 24 input types and 12 values of type: the details of most coercions are undocumented and subject to change.
is.vector() function in R
The is.vector() function returns TRUE if the input is a vector of the specified mode having no attributes other than names; otherwise, It returns FALSE.
Syntax
is.vector(obj, mode = "any")
Example
mtrx <- matrix(c(1:16), 4, 4)
vt <- as.vector(mtrx)
vt
is.vector(vt)
Output
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[1] TRUE
That is it for as.vector() function in R Programming.
See also
Convert R Character to Numeric

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.