# R中的数组用法完全剖析

## 创建数组

array()函数将创建一个采用向量的数组, 该向量是参数中的数字和维数(‘dim’)。

``````vector1 =  c (5, 10, 15, 20)
vector2 =  c (25, 30, 35, 40, 45, 50, 55, 60)
``````

``````final = array (c (array1, array2), dim =c(4, 4, 3))
print (final)
``````

``````, , 1

[, 1] [, 2] [, 3] [, 4]
[1, ]    5   25   45    5
[2, ]   10   30   50   10
[3, ]   15   35   55   15
[4, ]   20   40   60   20

, , 2

[, 1] [, 2] [, 3] [, 4]
[1, ]   25   45    5   25
[2, ]   30   50   10   30
[3, ]   35   55   15   35
[4, ]   40   60   20   40

, , 3

[, 1] [, 2] [, 3] [, 4]
[1, ]   45    5   25   45
[2, ]   50   10   30   50
[3, ]   55   15   35   55
[4, ]   60   20   40   60
``````

``````array1 =  c (9 , 18 )
array2 = c (27, 36)
r.names = c ("column1", "column2", "column3")
c.names = c ("row1", "row2")
m.names = c ("Arr1", "Arr2")

final = array (c (array1, array2), dim=c (2, 3, 2), dimnames=list (c.names, r.names, m.names))
print(final)
``````

``````, , Arr1

column1 column2 column3
row1       9      27       9
row2      18      36      18

, , Arr2

column1 column2 column3
row1      27       9      27
row2      36      18      36
``````

## 数组索引

``````a1=  c (1, 2, 3, 4)
a2= c (5, 6, 7, 8, 9)
r.names = c ("c1", "c2", "c3")
c.names = c ("r1", "r2", "r3")
m.names = c ("first")

arr = array (c (a1, a2), dim=c (3, 3, 1), dimnames=list (c.names, r.names, m.names))
print(arr)
``````
``````, , first

c1 c2 c3
r1  1  4  7
r2  2  5  8
r3  3  6  9
``````

`````` , , first

c1 c2 c3
r1  1  4  7
r2  2  5  8
r3  3  6  9
``````

1. 让我们从上面的数组” arr”中提取数字” 7″。
``````arr[1, 3, 1]
``````

7

1. 要访问1, 则从第1列和第1列的” arr [1, 1, 1]”行1中提取第一个数组” arr”。
``````arr[1, 1, 1]
``````

1个

1. 要一次访问多个值, 你需要指定所需的范围。
``````arr[1:2, 1:2, 1]
``````
c1 c2
11 1 4
22 2 5

``````    c1    c2
r1    1    4
r2    2    5
``````
1. 你可以使用以下语法访问整个数组’arr’, 其中’arr [, , 1]’指定包含所有行和列, 每个行和列均以逗号分隔, 并用空格表示。 1指定要提取的数组” arr”。
``````arr[ , , 1]
``````
c1 c2 c3
11 1 4 7
22 2 5 8
33 3 6 9

``````    c1    c2    c3
r1    1    4    7
r2    2    5    8
r3    3    6    9
``````
1. 你可以通过以下代码获得整个第二行, 其中arr [2, , 1]获取带有空格的第二行, 而1是要提取的” arr”。
``````arr[2, , 1]
``````
• c1
• 2
• c2
• 5
• c3
• 8

``````c1 2
c2 5
c3 8
``````
1. 你可以通过以下代码获得整个第二列, 其中arr [, 2, 1]空格, 第二列是2, 而1是要提取的” arr”。
``````mat[ , 2, 1]
``````
• 11
• 4
• 22
• 5
• 33
• 6

``````r1 4
r2 5
r3 6
``````

## 矩阵创建和添加

``````mat1 = arr[, , 1]
mat2 = arr[, , 1]
``````
``````   c1 c2 c3
r1  2  8 14
r2  4 10 16
r3  6 12 18
``````

``````final <- mat1+mat2
print(final)
``````

``````   c1 c2 c3
r1  2  8 14
r2  4 10 16
r3  6 12 18
``````

## apply()

” apply()”是R软件包之一, 它具有几个功能, 可帮助以更轻松有效的方式编写代码。你将在以下示例中看到该示例, 该示例可用于计算两个不同数组的总和。

apply()的语法为：

apply(x, margin, function)

x：作为矩阵的数组或二维数据。

margin：表示要用作边距值的函数, 行的c(1), 列的c(2), 行和列的c(1, 2)。

function：指示要在给定数据上应用的R内置或用户定义的函数。

``````array1 = c(5, 10, 15)
array2 = c(15, 20, 25, 30, 35, 40)
``````

``````my.Array <- array(c(array1, array2), dim = c(3, 3, 2))
print(my.Array)
``````
``````, , 1

[, 1] [, 2] [, 3]
[1, ]    5   15   30
[2, ]   10   20   35
[3, ]   15   25   40

, , 2

[, 1] [, 2] [, 3]
[1, ]    5   15   30
[2, ]   10   20   35
[3, ]   15   25   40
``````

``````, , 1

[, 1] [, 2] [, 3]
[1, ]    5   15   30
[2, ]   10   20   35
[3, ]   15   25   40

, , 2

[, 1] [, 2] [, 3]
[1, ]    5   15   30
[2, ]   10   20   35
[3, ]   15   25   40
``````

``````final <- apply(my.Array, c(2), sum)
print(final)
``````
``````[1]  60 120 210
``````

``````60 120 210
``````

``````final <- apply(my.Array, c(1), sum)
print(final)
``````
``````[1] 100 130 160
``````

``````100 130 160
``````

R数组

R数组函数和在R中创建数组

• 回顶