R语言 教程
- 第1节:R语言 概述
- 第2节:R语言 环境设置
- 第3节:R语言 基本语法
- 第4节:R语言 数据类型
- 第5节:R语言 变量
- 第6节:R语言 运算符
- 第7节:R语言 决策
- 第8节:R语言 包
- 第9节:R语言 循环
- 第10节:R语言 数据重塑
- 第11节:R语言 函数
- 第12节:R语言 字符串
- 第13节:R语言 向量
- 第14节:R语言 列表
- 第15节:R语言 矩阵
- 第16节:R语言 数组
- 第17节:R语言 因子
- 第18节:R语言 数据帧
- 第19节:R语言 条形图
- 第20节:R语言 箱线图
- 第21节:R语言 直方图
- 第22节:R语言 折线图
- 第23节:R语言 散点图
- 第24节:R语言 饼状图
- 第25节:R语言 CSV文件
- 第26节:R语言 Excel文件
- 第27节:R语言 二进制文件
- 第28节:R语言 XML文件
- 第29节:R语言 JSON文件
- 第30节:R语言 Web数据
- 第31节:R语言 数据库
- 第32节:R语言 平均值,中位数和模式
- 第33节:R语言 线性回归
- 第34节:R语言 多重回归
- 第35节:R语言 逻辑回归
- 第36节:R语言 标准分布
- 第37节:R语言 二项分布
- 第38节:R语言 泊松回归
- 第39节:R语言 协方差分析
- 第40节:R语言 时间序列分析
- 第41节:R语言 非线性最小二乘
- 第42节:R语言 决策树
- 第43节:R语言 随机森林算法
- 第44节:R语言 生存分析
- 第45节:R语言 卡方检验
R语言 数组
数组是可以在两个以上维度中存储数据的R数据对象。 例如 - 如果我们创建一个维度(2,3,4)的数组,则它创建4个矩形矩阵,每个矩阵具有2行和3列。 数组只能存储数据类型。
使用array()函数创建数组。 它使用向量作为输入,并使用dim参数中的值创建数组。
使用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