溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

R語(yǔ)言怎么創(chuàng)建數(shù)組

發(fā)布時(shí)間:2022-01-20 10:54:43 來(lái)源:億速云 閱讀:473 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“R語(yǔ)言怎么創(chuàng)建數(shù)組”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“R語(yǔ)言怎么創(chuàng)建數(shù)組”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

數(shù)組(array)是向量和矩陣的推廣,是多維(三維或三維以上)數(shù)據(jù)。與向量和矩陣一樣,數(shù)組的元素必須也是同一類型的數(shù)據(jù)。例如 - 如果我們創(chuàng)建一個(gè)維度(2,3,4)的數(shù)組,則會(huì)創(chuàng)建4個(gè)矩形矩陣,每個(gè)矩陣具有2行和3列。

創(chuàng)建數(shù)組

在R中,一般用array()函數(shù)來(lái)創(chuàng)建數(shù)組。array()的原型為:

array(data = NA, dim = length(data), dimnames = NULL)

其中:data給定數(shù)組元素,默認(rèn)情況下是NA;dim用來(lái)指定數(shù)組的維度,默認(rèn)情況下是一維數(shù)組;dimnames設(shè)定各維度的名稱,必須是個(gè)列表,默認(rèn)情況下無(wú)名稱。
例如創(chuàng)建一個(gè)由兩個(gè)3x3矩陣組成的數(shù)組,每個(gè)矩陣具有3行和3列。

# Create two vectors of different lengths.
vector1 <- c(2,3,5)
vector2 <- c(7,8,9,11,11,12)
# Take these vectors as input to the array.
result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)

運(yùn)行以上的代碼,輸出結(jié)果如下:

, , 1

     [,1] [,2] [,3]
[1,]    2    7   11
[2,]    3    8   11
[3,]    5    9   12

, , 2

     [,1] [,2] [,3]
[1,]    2    7   11
[2,]    3    8   11
[3,]    5    9   12

dimnames參數(shù)給數(shù)組中的行,列和矩陣命名。

# Create two vectors of different lengths.
vector1 <- c(2,3,5)
vector2 <- c(7,8,9,11,11,12)
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)

運(yùn)行以上的代碼,輸出結(jié)果如下:

, , Matrix1

     COL1 COL2 COL3
ROW1    2    7   11
ROW2    3    8   11
ROW3    5    9   12

, , Matrix2

     COL1 COL2 COL3
ROW1    2    7   11
ROW2    3    8   11
ROW3    5    9   12

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

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

# Create two vectors of different lengths.
vector1 <- c(2,3,5)
vector2 <- c(7,8,9,11,11,12)
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])

運(yùn)行以上的代碼,輸出結(jié)果如下:

COL1 COL2 COL3
   5    9   12

[1] 11

     COL1 COL2 COL3
ROW1    2    7   11
ROW2    3    8   11
ROW3    5    9   12

操縱數(shù)組元素

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

# Create two vectors of different lengths.
vector1 <- c(2,3,5)
vector2 <- c(7,8,9,11,11,12)

# 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(4,1,0)
vector4 <- c(6,0,7,3,13,3,2,8,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)

運(yùn)行以上的代碼,輸出結(jié)果如下:

     [,1] [,2] [,3]
[1,]    4   14   22
[2,]    6   16   22
[3,]   10   18   24

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

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

apply(x, margin, fun)

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

  • x - 是一個(gè)數(shù)組。

  • margin - 是使用的數(shù)據(jù)集的名稱。

  • fun - 是應(yīng)用于數(shù)組元素的函數(shù)。

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

# Create two vectors of different lengths.
vector1 <- c(2,3,5)
vector2 <- c(7,8,9,11,11,12)

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

運(yùn)行以上的代碼,輸出結(jié)果如下:

, , 1

     [,1] [,2] [,3]
[1,]    2    7   11
[2,]    3    8   11
[3,]    5    9   12

, , 2

     [,1] [,2] [,3]
[1,]    2    7   11
[2,]    3    8   11
[3,]    5    9   12

[1] 40 44 52

讀到這里,這篇“R語(yǔ)言怎么創(chuàng)建數(shù)組”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI