- R 教程
- R - 首頁
- R - 概述
- R - 環境設定
- R - 基本語法
- R - 資料型別
- R - 變數
- R - 運算子
- R - 決策
- R - 迴圈
- R - 函式
- R - 字串
- R - 向量
- R - 列表
- R - 矩陣
- R - 陣列
- R - 因子
- R - 資料框
- R - 包
- R - 資料重塑
R - 陣列
陣列是 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
廣告