How to create an array from vectors in R ?
Last Updated :
26 Jan, 2022
In this article, we will discuss how to create an array from the vectors in R Programming Language. We can create an array using array() function. We have to pass the vectors and dim() as parameters. Here dim() function is used to give dimensions for the array.
Syntax:
array_name = array( c(vector 1 , vector 2 , . . . , vector n) , dim=c( no.of rows , no.of columns , no.of arrays ))
The approach is straightforward first we need to create vectors and then pass them to the array() function to create one. Implementation is given below:
Example 1 :
R
vec1= c (1:5)
vec2= c (6:10)
arr= array ( c (vec1,vec2),dim= c (2,5,3))
print (arr)
|
Output :
Example 2 :
R
vec1= c (1:10)
vec2= c (11:20)
arr= array ( c (vec1,vec2),dim= c (3,3,3))
print (arr)
|
Output :
Example 3 :
R
vec1= c (1:9)
vec2= c (10:27)
arr= array ( c (vec1,vec2),dim= c (2,3,3))
print (arr)
|
Output :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...