美文网首页
R语言 数组

R语言 数组

作者: yuanyb | 来源:发表于2017-11-06 16:31 被阅读0次

数组是可以在两个以上维度中存储数据的R数据对象。 例如 - 如果我们创建一个维度(2,3,4)的数组,则它创建4个矩形矩阵,每个矩阵具有2行和3列。 数组只能存储数据类型。
使用array()函数创建数组。 它使用向量作为输入,并使用dim参数中的值创建数组。

以下示例创建一个由两个3x3矩阵组成的数组,每个矩阵具有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(row.names,column.names,
matrix.names))
print(result)
当我们执行上面的代码,它产生以下结果 -
, , 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
访问数组元素

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(row.names,
column.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])
当我们执行上面的代码,它产生以下结果 -
COL1 COL2 COL3
3 12 15
[1] 13
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 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+matrix2
print(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

相关文章

  • R语言 数组

    数组是可以在两个以上维度中存储数据的R数据对象。 例如 - 如果我们创建一个维度(2,3,4)的数组,则它创建4个...

  • Day5—刘强

    R语言的数据结构 数据类型 R语言主要数据类型如下: 向量(vector) 矩阵(Matrix) 数组(Array...

  • R 语言-矩阵与数组

    1、矩阵 矩阵(Matrix)是一个按照长方阵列排列的复数或实数集合。向量是一维的,而矩阵是二维的,需要有行和列。...

  • R语言——向量、矩阵、数组

    向量 向量是用于存储数值型、字符型或逻辑型数据的一维数组。执行组合功能的函数c()可用来创建向量。 其中,a是数值...

  • 学习小组Day5笔记——四海

    R语言第二天——R基础 数据类型 向量 vector* #重要 矩阵 matrix 数组 array 数据框 da...

  • 2 - 常用集合

    数组 Go 中的数组和其他语言数组的初始化类似 你必须指定数组的长度,你可以用 ... 代替这个长度 你可以用 r...

  • R语言中数组的使用

    在R语言中,可以认为数组是矩阵的扩展,它将矩阵扩展到2维以上。如果给定的数组是1维的则相当于向量,2维的相当于矩阵...

  • R语言矩阵与数组2021.1.24

    总结一句,放在开头:R语言中的向量更像是数组,而数组更像是矩阵 。 矩阵 矩阵(Matrix)是一个按照长方阵列排...

  • R语言入门笔记(2) - R语言科学计算

    R语言科学计算 分类统计 mean(),求平均值 min(),求最小值 sd(),求标准差 数组和矩阵 数组与矩阵...

  • RNA-seq分析(三)DESeq2

    1、DESeq2差异表达分析 R语言数据类型:向量(vector);列表(list);矩阵(matrix);数组(...

网友评论

      本文标题:R语言 数组

      本文链接:https://www.haomeiwen.com/subject/uxblmxtx.html