小白教程
所有教程
关于
Search
172.70.127.6
172.70.127.6
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“R 数组”的源代码
本页内容
上一节:
R_矩阵
下一节:
R_因子
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:R 数组}}[[Category:R 教程|13]] = R 数组 = 数组也是 R 语言的对象,R 语言可以创建一维或多维数组。 R 语言数组是一个同一类型的集合,前面我们学的矩阵 matrix 其实就是一个二维数组。 向量、矩阵、数组关系可以看下图: R 语言数组创建使用 array() 函数,该函数使用向量作为输入参数,可以使用 dim 设置数组维度。 array() 函数语法格式如下: <sample title="" desc="" lang="r" hererun="1"> array(data = NA, dim = length(data), dimnames = NULL) </sample> '''参数说明:''' * data 向量,数组元素。 * dim 数组的维度,默认是一维数组。 * dimnames 维度的名称,必须是个列表,默认情况下是不设置名称的。 以下实例我们创建一个 3 行 3 列的的二维数组: <sample title="" desc="" lang="r" hererun="1"> # 创建两个不同长度的向量 vector1 <- c(5,9,3) vector2 <- c(10,11,12,13,14,15) # 创建数组 result <- array(c(vector1,vector2),dim = c(3,3,2)) print(result) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> , , 1 [,1] [,2] [,3] [1,] 5 10 13 [2,] 9 11 14 [3,] 3 12 15 , , 2 [,1] [,2] [,3] [1,] 5 10 13 [2,] 9 11 14 [3,] 3 12 15 </sample> 使用 dimnames 参数来设置各个维度的名称:: <sample title="" desc="" lang="r" hererun="1"> # 创建两个不同长度的向量 vector1 <- c(5,9,3) vector2 <- c(10,11,12,13,14,15) column.names <- c("COL1","COL2","COL3") row.names <- c("ROW1","ROW2","ROW3") matrix.names <- c("Matrix1","Matrix2") # 创建数组,并设置各个维度的名称 result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,matrix.names)) print(result) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> , , Matrix1 COL1 COL2 COL3 ROW1 5 10 13 ROW2 9 11 14 ROW3 3 12 15 , , Matrix2 COL1 COL2 COL3 ROW1 5 10 13 ROW2 9 11 14 ROW3 3 12 15 </sample> === 访问数组元素 === 如果想获取数组元素,可以通过使用元素的列索引和行索引,类似坐标形式。 <sample title="" desc="" lang="r" hererun="1"> # 创建两个不同长度的向量 vector1 <- c(5,9,3) vector2 <- c(10,11,12,13,14,15) column.names <- c("COL1","COL2","COL3") row.names <- c("ROW1","ROW2","ROW3") matrix.names <- c("Matrix1","Matrix2") # 创建数组 result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names, column.names, matrix.names)) # 显示数组第二个矩阵中第三行的元素 print(result[3,,2]) # 显示数组第一个矩阵中第一行第三列的元素 print(result[1,3,1]) # 输出第二个矩阵 print(result[,,2]) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> COL1 COL2 COL3 3 12 15 [1] 13 COL1 COL2 COL3 ROW1 5 10 13 ROW2 9 11 14 ROW3 3 12 15 </sample> === 操作数组元素 === 由于数组是由多个维度的矩阵组成,所以我们可以通过访问矩阵的元素来访问数组元素。 <sample title="" desc="" lang="r" hererun="1"> # 创建两个不同长度的向量 vector1 <- c(5,9,3) vector2 <- c(10,11,12,13,14,15) # 创建数组 array1 <- array(c(vector1,vector2),dim = c(3,3,2)) # 创建两个不同长度的向量 vector3 <- c(9,1,0) vector4 <- c(6,0,11,3,14,1,2,6,9) array2 <- array(c(vector1,vector2),dim = c(3,3,2)) # 从数组中创建矩阵 matrix1 <- array1[,,2] matrix2 <- array2[,,2] # 矩阵相加 result <- matrix1+matrix2 print(result) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> [,1] [,2] [,3] [1,] 10 20 26 [2,] 18 22 28 [3,] 6 24 30 </sample> 另外我们可以使用 '''apply()''' 元素对数组元素进行跨维度计算,语法格式如下: <sample title="" desc="" lang="r" hererun="1"> apply(x, margin, fun) </sample> 参数说明: * '''x''' 数组 * '''margin''' 数据名称 * '''fun''' 计算函数 以下我们使用 apply() 函数来计算数组两个矩阵中每一行对数字之和。 <sample title="" desc="" lang="r" hererun="1"> # 创建两个不同长度的向量 vector1 <- c(5,9,3) vector2 <- c(10,11,12,13,14,15) # 创建数组 new.array <- array(c(vector1,vector2),dim = c(3,3,2)) print(new.array) # 计算数组中所有矩阵第一行的数字之和 result <- apply(new.array, c(1), sum) print(result) </sample> 执行以上代码输出结果为: <sample title="" desc="" lang="r" hererun="1"> , , 1 [,1] [,2] [,3] [1,] 5 10 13 [2,] 9 11 14 [3,] 3 12 15 , , 2 [,1] [,2] [,3] [1,] 5 10 13 [2,] 9 11 14 [3,] 3 12 15 [1] 56 68 60 </sample>
返回至“
R 数组
”。
上一节:
R_矩阵
下一节:
R_因子