欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語言數(shù)組實(shí)例用法及知識點(diǎn)總結(jié)

 更新時(shí)間:2021年04月11日 09:48:16   作者:w3cschool  
在本文里,我們給大家整理的是關(guān)于R語言數(shù)組的相關(guān)知識點(diǎn),有興趣的朋友們可以跟著學(xué)習(xí)參考下。

數(shù)組是可以在兩個(gè)以上維度中存儲(chǔ)數(shù)據(jù)的R數(shù)據(jù)對象。 例如 - 如果我們創(chuàng)建一個(gè)維度(2,3,4)的數(shù)組,則它創(chuàng)建4個(gè)矩形矩陣,每個(gè)矩陣具有2行和3列。 數(shù)組只能存儲(chǔ)數(shù)據(jù)類型。
使用array()函數(shù)創(chuàng)建數(shù)組。 它使用向量作為輸入,并使用dim參數(shù)中的值創(chuàng)建數(shù)組。

以下示例創(chuàng)建一個(gè)由兩個(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í)行上面的代碼,它產(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í)行上面的代碼,它產(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

訪問數(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 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í)行上面的代碼,它產(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òu)成矩陣,所以對數(shù)組元素的操作通過訪問矩陣的元素來執(zhí)行。

# 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í)行上面的代碼,它產(chǎn)生以下結(jié)果 -

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

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

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

語法

apply(x, margin, fun)

以下是所使用的參數(shù)的說明

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

我們使用下面的apply()函數(shù)計(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í)行上面的代碼,它產(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

到此這篇關(guān)于R語言數(shù)組實(shí)例用法及知識點(diǎn)總結(jié)的文章就介紹到這了,更多相關(guān)R語言數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • ComplexHeatmap繪制單個(gè)熱圖

    ComplexHeatmap繪制單個(gè)熱圖

    這篇文章主要為大家介紹了ComplexHeatmap繪制單個(gè)熱圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • R語言數(shù)據(jù)結(jié)構(gòu)之矩陣、數(shù)組與數(shù)據(jù)框詳解

    R語言數(shù)據(jù)結(jié)構(gòu)之矩陣、數(shù)組與數(shù)據(jù)框詳解

    進(jìn)行數(shù)據(jù)分析的第一步是先拿到數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于R語言數(shù)據(jù)結(jié)構(gòu)之矩陣、數(shù)組與數(shù)據(jù)框的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • R語言刪除/添加數(shù)據(jù)框中的某一行/列

    R語言刪除/添加數(shù)據(jù)框中的某一行/列

    這篇文章主要介紹了R語言刪除/添加數(shù)據(jù)框中的某一行/列,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • R語言ggplot2?title設(shè)置教程(main,axis和legend?titles)

    R語言ggplot2?title設(shè)置教程(main,axis和legend?titles)

    ggplot2是一個(gè)強(qiáng)大的作圖工具,它可以讓你不受現(xiàn)有圖形類型的限制,創(chuàng)造出任何有助于解決你所遇到問題的圖形,下面這篇文章主要給大家介紹了關(guān)于R語言ggplot2?title設(shè)置(main,axis和legend?titles)的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • R語言的歷史介紹

    R語言的歷史介紹

    在本篇內(nèi)容里小編給大家介紹了關(guān)于R語言的歷史內(nèi)容,有興趣學(xué)習(xí)的朋友可以閱讀下。
    2021-03-03
  • R語言實(shí)現(xiàn)導(dǎo)出矩陣

    R語言實(shí)現(xiàn)導(dǎo)出矩陣

    這篇文章主要介紹了R語言實(shí)現(xiàn)導(dǎo)出矩陣,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言創(chuàng)建矩陣的實(shí)現(xiàn)方法

    R語言創(chuàng)建矩陣的實(shí)現(xiàn)方法

    這篇文章主要介紹了R語言創(chuàng)建矩陣的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • R語言 數(shù)據(jù)集行列互換的技巧分享

    R語言 數(shù)據(jù)集行列互換的技巧分享

    這篇文章主要介紹了R語言 數(shù)據(jù)集行列互換的技巧分享,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • R語言繪制雙坐標(biāo)圖的案例詳解

    R語言繪制雙坐標(biāo)圖的案例詳解

    這篇文章主要介紹了R語言繪制雙坐標(biāo)圖,下面就跟大家介紹plotrix包中的twoord.plot()函數(shù)和twoord.stackplot()函數(shù),它們可以實(shí)現(xiàn)雙坐標(biāo)軸圖形的繪制,需要的朋友可以參考下
    2023-01-01
  • R語言對Excel文件操作實(shí)例

    R語言對Excel文件操作實(shí)例

    在本篇文章里小編給大家整理了一篇關(guān)于R語言對Excel文件操作實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05

最新評論