R数组用法详解

本文概述

  • R数组语法
  • 如何创建?
  • 命名行和列
  • 访问数组元素
  • 元素操纵
  • 跨数组元素的计算
在R中, 数组是数据对象, 可让我们以二维方式存储数据。在R中, 借助于array()函数创建一个数组。此array()函数将向量作为输入, 并使用dim参数中的向量值创建一个数组。
例如, 如果我们将创建一个尺寸为(2、3、4)的数组, 则它将创建4个2行3列的矩形矩阵。
R数组语法 R数组具有以下语法:
array_name < - array(data, dim= (row_size, column_size, matrices, dim_names))

数据
数据是array()函数中的第一个参数。它是输入向量, 被提供给数组。
矩阵
在R中, 数组由多维矩阵组成。
row_size
此参数定义数组可以存储的行元素的数量。
column_size
此参数定义数组可以存储的列元素的数量。
dim_names
此参数用于更改行和列的默认名称。
R数组用法详解

文章图片
如何创建? 在R中, 数组创建非常简单。我们可以使用vector和array()函数轻松创建一个数组。在数组中, 数据以矩阵形式存储。创建矩阵只有两个步骤, 如下所示
  1. 第一步, 我们将创建两个不同长度的向量。
  2. 一旦创建了向量, 我们就将这些向量作为数组的输入。
让我们看一个示例, 以了解如何在vectors和array()函数的帮助下实现数组。
例子
#Creating two vectors of different lengthsvec1 < -c(1, 3, 5)vec2 < -c(10, 11, 12, 13, 14, 15)#Taking these vectors as input to the array res < - array(c(vec1, vec2), dim=c(3, 3, 2))print(res)

输出
, , 1[, 1] [, 2] [, 3][1, ]11013[2, ]31114[3, ]51215, , 2[, 1] [, 2] [, 3][1, ]11013[2, ]31114[3, ]51215

命名行和列 在R中, 我们可以为数组的行, 列和矩阵指定名称。这是通过array()函数的dim name参数完成的。
不必为行和列指定名称。它仅用于区分行和列, 以更好地理解。
【R数组用法详解】下面是一个示例, 其中我们创建两个数组, 并为行, 列和矩阵命名。
例子
#Creating two vectors of different lengthsvec1 < -c(1, 3, 5)vec2 < -c(10, 11, 12, 13, 14, 15)#Initializing names for rows, columns and matricescol_names < - c("Col1", "Col2", "Col3")row_names < - c("Row1", "Row2", "Row3")matrix_names < - c("Matrix1", "Matrix2")#Taking the vectors as input to the array res < - array(c(vec1, vec2), dim=c(3, 3, 2), dimnames=list(row_names, col_names, matrix_names))print(res)

输出
, , Matrix1Col1 Col2 Col3Row111013Row231114Row351215, , Matrix2Col1 Col2 Col3Row111013Row231114Row351215

访问数组元素 像C或C ++一样, 我们可以访问数组的元素。借助索引访问元素。简单来说, 我们可以在索引方法的帮助下访问数组的元素。让我们看一个例子, 以了解如何使用索引方法访问数组的元素。
例子
, , Matrix1Col1 Col2 Col3Row111013Row231114Row351215, , Matrix2Col1 Col2 Col3Row111013Row231114Row351215Col1 Col2 Col351215[1] 13Col1 Col2 Col3Row111013Row231114Row351215

元素操纵 该数组由多个维度的矩阵组成, 因此可以通过访问矩阵的元素来执行对数组元素的操作。
例子
#Creating two vectors of different lengthsvec1 < -c(1, 3, 5)vec2 < -c(10, 11, 12, 13, 14, 15)#Taking the vectors as input to the array1 res1 < - array(c(vec1, vec2), dim=c(3, 3, 2))print(res1)#Creating two vectors of different lengthsvec1 < -c(8, 4, 7)vec2 < -c(16, 73, 48, 46, 36, 73)#Taking the vectors as input to the array2 res2 < - array(c(vec1, vec2), dim=c(3, 3, 2))print(res2)#Creating matrices from these arraysmat1 < - res1[, , 2]mat2 < - res2[, , 2]res3 < - mat1+mat2print(res3)

输出
, , 1[, 1] [, 2] [, 3][1, ]11013[2, ]31114[3, ]51215, , 2[, 1] [, 2] [, 3][1, ]11013[2, ]31114[3, ]51215, , 1[, 1] [, 2] [, 3][1, ]81646[2, ]47336[3, ]74873, , 2[, 1] [, 2] [, 3][1, ]81646[2, ]47336[3, ]74873[, 1] [, 2] [, 3][1, ]92659[2, ]78450[3, ]126088

跨数组元素的计算 出于计算目的, r提供了apply()函数。此Apply函数包含三个参数, 即x, margin和function。
此函数采用我们必须在其上执行计算的数组。 apply()函数的基本语法如下:
apply(x, margin, fun)

在此, x是一个数组, 一个空白是使用的数据集的名称, fun是要应用于该数组的元素的函数。
例子
#Creating two vectors of different lengthsvec1 < -c(1, 3, 5)vec2 < -c(10, 11, 12, 13, 14, 15)#Taking the vectors as input to the array1 res1 < - array(c(vec1, vec2), dim=c(3, 3, 2))print(res1)#using apply function result < - apply(res1, c(1), sum)print(result)

输出
, , 1[, 1] [, 2] [, 3][1, ]11013[2, ]31114[3, ]51215, , 2[, 1] [, 2] [, 3][1, ]11013[2, ]31114[3, ]51215[1] 48 56 64

    推荐阅读