在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ R語(yǔ)言數(shù)組
R語(yǔ)言列表
R語(yǔ)言隨機(jī)森林
R語(yǔ)言矩陣
R語(yǔ)言邏輯回歸
R語(yǔ)言數(shù)據(jù)幀
R語(yǔ)言數(shù)據(jù)重塑
R語(yǔ)言概述
R語(yǔ)言包
R語(yǔ)言字符串
R語(yǔ)言CSV文件
R語(yǔ)言運(yùn)算符
為什么使用R語(yǔ)言做統(tǒng)計(jì)?
R語(yǔ)言Web數(shù)據(jù)
R語(yǔ)言二進(jìn)制文件
R語(yǔ)言XML文件
R語(yǔ)言JSON文件
R語(yǔ)言因子
R語(yǔ)言容易學(xué)習(xí)嗎?
R語(yǔ)言基礎(chǔ)語(yǔ)法
R語(yǔ)言向量
R語(yǔ)言教程
R語(yǔ)言正態(tài)分布
R語(yǔ)言平均值,中位數(shù)和眾數(shù)
R語(yǔ)言變量
R語(yǔ)言條形圖
R語(yǔ)言決策樹(shù)
R語(yǔ)言開(kāi)發(fā)環(huán)境安裝配置
R語(yǔ)言數(shù)組
R語(yǔ)言數(shù)據(jù)類(lèi)型
R語(yǔ)言非線性最小二乘法
R語(yǔ)言直方圖
R語(yǔ)言卡方檢驗(yàn)
R語(yǔ)言泊松回歸
R語(yǔ)言決策結(jié)構(gòu)
R語(yǔ)言盒形圖(箱形圖)
R語(yǔ)言協(xié)方差分析
R語(yǔ)言二項(xiàng)分布
R語(yǔ)言餅狀圖
R語(yǔ)言循環(huán)
R語(yǔ)言散點(diǎn)圖
R語(yǔ)言線性回歸
R語(yǔ)言時(shí)間序列分析
R語(yǔ)言線形圖
R語(yǔ)言在現(xiàn)實(shí)中的應(yīng)用
R語(yǔ)言生存分析
R語(yǔ)言多元(多重)回歸
R語(yǔ)言函數(shù)
R語(yǔ)言Excel文件
R語(yǔ)言連接數(shù)據(jù)庫(kù)(MySQL)

R語(yǔ)言數(shù)組

數(shù)組是可以在二維及以上存儲(chǔ)數(shù)據(jù)的R數(shù)據(jù)對(duì)象。 例如 - 如果創(chuàng)建一個(gè)維數(shù)組(2,3,4),那么它將創(chuàng)建4個(gè)矩形矩陣,每個(gè)矩陣具有2行和3列。數(shù)組只能存儲(chǔ)數(shù)據(jù)類(lèi)型。

數(shù)組可通過(guò)使用array()函數(shù)來(lái)創(chuàng)建。 它將向量作為輸入,并使用dim參數(shù)中的值來(lái)創(chuàng)建數(shù)組。

例子

以下示例創(chuàng)建兩個(gè)3x3矩陣的數(shù)組,每個(gè)矩陣具有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)

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

, , 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參數(shù)為數(shù)組中的行,列和矩陣命名。

# 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)

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

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

訪問(wèn)數(shù)組元素

有關(guān)如何訪問(wèn)數(shù)組元素,請(qǐng)參考以下代碼實(shí)現(xiàn) -

# 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])

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

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

操縱數(shù)組元素

由于數(shù)組是由多個(gè)維度組成的矩陣,通過(guò)訪問(wèn)矩陣的元素來(lái)執(zhí)行數(shù)組元素的相關(guān)操作。

# 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)

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

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

跨數(shù)組元素的計(jì)算

我們可以使用apply()函數(shù)對(duì)數(shù)組中的元素進(jìn)行計(jì)算。

語(yǔ)法

apply(x, margin, fun)

以下是使用的參數(shù)的描述 -

  • x - 是一個(gè)數(shù)組。
  • margin - 是使用的數(shù)據(jù)集的名稱(chēng)。
  • fun - 是應(yīng)用于數(shù)組元素的函數(shù)。

例子

使用下面的apply()函數(shù)來(lái)計(jì)算所有矩陣中數(shù)組的行中的元素的總和。

# 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)

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

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