打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
R语言数组array函数

数组是一个可以在两个以上的维度存储数据的R数据对象。例如 - 如果创建尺寸(2,3,4)的数组,那么创建4个矩形矩阵每2行3列。数组只能存储数据类型。

使用 array()函数创建数组。它需要向量作为输入,并使用 dim 参数的值,以创建一个数组。

示例

例子下面将创建的每两个3×3矩阵的数组,具有3行3列。

# Create two vectors of different lengths.vector1 <- c(5,9,3)vector2 <- c(10,11,12,13,14,15)# Take these vectors as input to the array.result <- array(c(vector1,vector2),dim=c(3,3,2))print(result)

当我们上面的代码执行时,它产生以下结果:

, , 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

命名列和行

我们可以通过使用dimnames参数给予名称添加到数组中的行,列和矩阵。

# Create two vectors of different lengths.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")# Take these vectors as input to the array.result <- array(c(vector1,vector2),dim=c(3,3,2),dimnames = list(column.names,row.names,matrix.names))print(result)

当我们上面的代码执行时,它产生以下结果:

, , Matrix1     ROW1 ROW2 ROW3COL1    5   10   13COL2    9   11   14COL3    3   12   15, , Matrix2     ROW1 ROW2 ROW3COL1    5   10   13COL2    9   11   14COL3    3   12   15

访问数组元素

# Create two vectors of different lengths.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")# Take these vectors as input to the array.result <- array(c(vector1,vector2),dim=c(3,3,2),dimnames = list(column.names,row.names,matrix.names))# Print the third row of the second matrix of the array.print(result[3,,2])# Print the element in the 1st row and 3rd column of the 1st matrix.print(result[1,3,1])# Print the 2nd Matrix.print(result[,,2])

当我们上面的代码执行时,它产生以下结果:

ROW1 ROW2 ROW3    3   12   15 [1] 13     ROW1 ROW2 ROW3COL1    5   10   13COL2    9   11   14COL3    3   12   15

操纵数组元素

作为数组由矩阵中多个维度上数组的元素的操作,是由访问矩阵的元素进行。

# Create two vectors of different lengths.vector1 <- c(5,9,3)vector2 <- c(10,11,12,13,14,15)# Take these vectors as input to the array.array1 <- array(c(vector1,vector2),dim=c(3,3,2))# Create two vectors of different lengths.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))# create matrices from these arrays.matrix1 <- array1[,,2]matrix2 <- array2[,,2]# Add the matrices.result <- matrix1+matrix2print(result)

当我们上面的代码执行时,它产生以下结果:

     [,1] [,2] [,3][1,]   10   20   26[2,]   18   22   28[3,]    6   24   30

跨越数组元素计算

我们可以用 apply()函数在一个数组做跨越元素计算。

语法

apply(x, margin, fun)

以下是所使用的参数的说明:

  • x - 是一个数组
  • margin - 是所使用的数据集的名称
  • fun - 是在数组中的元素应用的函数

示例

我们使用下面的 apply()函数来计算在所有矩阵中的阵列的行中的元素的总和。

# Create two vectors of different lengths.vector1 <- c(5,9,3)vector2 <- c(10,11,12,13,14,15)# Take these vectors as input to the array.new.array <- array(c(vector1,vector2),dim=c(3,3,2))print(new.array)# Use apply to calculate the sum of the rows across all the matrices.result <- apply(new.array, c(1), sum)print(result) 

当我们上面的代码执行时,它产生以下结果:

, , 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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
斯坦福CS231n深度学习课程笔记翻译(一)
Python中基础使用及Numpy、Scipy、Matplotlib 使用教程
R语言笔记丨矩阵、数组介绍
eigen与matlab对应函数列表
在Visual C++ 6.0上实现矩阵的各种运算
Eigen常用函数以及注意事项总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服