数组是一个可以在两个以上的维度存储数据的R数据对象。例如 - 如果创建尺寸(2,3,4)的数组,那么创建4个矩形矩阵每2行3列。数组只能存储数据类型。

使用 array()函数创建数组。它需要向量作为输入,并使用 dim 参数的值,以创建一个数组。

示例

例子下面将创建的每两个3×3矩阵的数组,具有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(column.names,row.names,matrix.names))
print(result)

当我们上面的代码执行时,它产生以下结果:

, , Matrix1

     ROW1 ROW2 ROW3
COL1 5 10 13
COL2 9 11 14
COL3 3 12 15 , , Matrix2 ROW1 ROW2 ROW3
COL1 5 10 13
COL2 9 11 14
COL3 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(column.names,row.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])

当我们上面的代码执行时,它产生以下结果:

ROW1 ROW2 ROW3
3 12 15
[1] 13
ROW1 ROW2 ROW3
COL1 5 10 13
COL2 9 11 14
COL3 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

R语言数组array函数的更多相关文章

  1. php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】

    本文实例总结了php常用数组array函数.分享给大家供大家参考,具体如下: array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 案例: <?php ...

  2. R语言:常用函数【转】

    数据结构 一.数据管理vector:向量 numeric:数值型向量 logical:逻辑型向量 character:字符型向量list:列表 data.frame:数据框 c:连接为向量或列表len ...

  3. C语言数组和函数实例练习(二)

    1.C语言中不允许函数的嵌套定义,但可以使用函数的嵌套调用. 例1:输入4个整数,找出其中最大的数. #include <stdio.h> #include <stdlib.h> ...

  4. C语言数组和函数实例练习(一)

    C语言的数组和函数部分的知识,在语法上和Java语法是有所相似的,这里只通过实例总结一些自己感觉需要理解的部分知识. 1.数组 数组中的元素具有相同的数据类型:数组一旦创建,不能被改变:数组中元素在内 ...

  5. R语言 三个函数sort();rank();order()

    R语言入门,弄懂了几个简单的函数,分享一下:R语言排序有几个基本函数: sort():rank():order()sort()是对向量进行从小到大的排序rank()返回的是对向量中每个数值对应的秩or ...

  6. C语言 数组做函数参数退化为指针的技术推演

    //数组做函数参数退化为指针的技术推演 #include<stdio.h> #include<stdlib.h> #include<string.h> //一维数组 ...

  7. ​C语言数组作为函数参数

    数组可以作为函数的参数使用,进行数据传送. 数组用作函数参数有两种形式,一种是把数组元素(下标变量)作为实参使用:另一种是把数组名作为函数的形参和实参使用. 数组元素作函数实参 数组元素就是下标变量, ...

  8. R语言——基本绘图函数

    通过一个综合的例子测试绘图函数 学习的内容是tigerfish老师的教程. 第一节:基本知识 用seq函数产生100位学生的学号. > num = seq(,) > num [] [] [ ...

  9. R语言do.call 函数用法详解

    虽然R语言有类型很丰富的数据结构,但是很多时候数据结构比较复杂,那么基本就会用到list这种结构的数据类型.但是list对象很难以文本的形式导出,因此需要一个函数能快速将复杂的list结构扁平化成da ...

随机推荐

  1. elasticsearch不能使用root启动问题解决

    问题: es安装好之后,使用root启动会报错:can not run elasticsearch as root [root@iZbp1bb2egi7w0ueys548pZ bin]# ./elas ...

  2. OpenSSH服务及其相关应用

    远程登录工具: telnet,TCP/23:认证明文,数据传输明文,不够安全,所以出现了ssh ssh:Secure SHell,TCP/22,刚开始免费,后来商业化了,所以出现了Openssh,这个 ...

  3. JAVA实现长连接(含心跳检测)Demo

    实现原理: 长连接的维持,是要客户端程序,定时向服务端程序,发送一个维持连接包的.       如果,长时间未发送维持连接包,服务端程序将断开连接. 客户端:       Client通过持有Sock ...

  4. vsftp -samba-autofs

    摘要: 1.FTP文件传输协议,PAM可插拔认证模块,TFTP简单文件传输协议. 注意:iptables防火墙管理工具默认禁止了FTP传输协议的端口号 2.vsftpd服务程序三种认证模式?三种认证模 ...

  5. ubuntu emacs的安装

    在终端依次输入这三条命令即可 sudo add-apt-repository ppa:ubuntu-elisp/ppa sudo apt-get update sudo apt-get install ...

  6. javascript学习日记1

    1.JavaScript:写入 HTML 输出 document.write("<h1>This is a heading</h1>"); document ...

  7. $.ajax()——超时设置,增加 loading 提升体验

    前端发送Ajax请求到服务器,服务器返回数据这一过程,因原因不同耗时长短也有差别,且这段时间内页面显示空白.如何优化这段时间内的交互体验,以及长时间内服务器仍未返回数据这一问题,是我们开发中不容忽视的 ...

  8. [Objective-C语言教程]指针(15)

    Objective-C中的指针简单易学.使用指针可以更轻松地执行某些Objective-C编程任务,并且在不使用指针的情况下无法执行其他任务(如动态内存分配). 所以有必要学习指向成为一个完美的Obj ...

  9. Microsoft Visual C++ 14.0 is required

    building 'twisted.test.raiser' extensionerror: Microsoft Visual C++ 14.0 is required. Get it with &q ...

  10. notepad++常用操作梳理

    在  设置---管理快捷键   可以查询/重置快捷键.如下: 工作or学习中最长用到的操作: Ctrl+ALT-C:列编辑Ctrl+U:转换为小写Ctrl+Shift+U:转换为大写Ctrl+B:跳转 ...