如何从 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:
R
# 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
# intp array() function
arr=array(c(vec1,vec2),dim=c(2,5,3))
# printing the array
print(arr)
R
# creating vector with 1
# to 10 values
vec1=c(1:10)
# creating vector with 11
# to 20 values
vec2=c(11:20)
# passing vectors intp the
# array() function .
arr=array(c(vec1,vec2),dim=c(3,3,3))
# printing the array
print(arr)
R
# 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)
输出 :
示例 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 intp 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)
输出 :