R语言 如何从向量创建一个数组
在这篇文章中,我们将讨论如何在R编程语言中从向量创建一个数组。我们可以使用array()函数创建一个数组。我们必须传递向量和dim()作为参数。这里dim()函数被用来为数组提供尺寸。
语法 。
array_name = array( c(vector 1 , vector 2 , . . , vector n) , dim=c( no.of rows , no.of columns , no.of arrays ))
这个方法很直接,首先我们需要创建向量,然后把它们传给array()函数来创建一个。下面给出了实现方法。
例1 :
# creating the vector with
# 1 to 5 values
vec1=c(1:5)
# creating the vector with
# 6 to 10 values
vec2=c(6:10)
# passing the vectors as parameters
# into array() function
arr=array(c(vec1,vec2),dim=c(2,5,3))
# printing the array
print(arr)
输出:
例2 :
# creating vector with 1
# to 10 values
vec1=c(1:10)
# creating vector with 11
# to 20 values
vec2=c(11:20)
# passing vectors into the
# array() function .
arr=array(c(vec1,vec2),dim=c(3,3,3))
# printing the array
print(arr)
输出:
例3 :
# creating vector with 1
# to 9 values
vec1=c(1:9)
# creating vector with 11
# to 27 values
vec2=c(10:27)
# passing the two vectors into
# array() function
arr=array(c(vec1,vec2),dim=c(2,3,3))
# printing the array
print(arr)
输出: