数组是一个可以在两个以上的维度存储数据的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. 自定义TFS工作项“所有链接”列表中的列

    这个功能只有使用团队资源管理器查看工作项才有

  2. 使用centos官方镜像制作jdk8环境镜像

    首先将jdk文件或者tar包放在/var/local路径下 然后Dockerfile中写 # 以 centos7 为基础镜像 FROM centos:latest MAINTAINER chen # ...

  3. ASP.NET 邮件发送

    ASP.NET 邮件发送用NET的MAIL类即可实现. 邮件发时,为不影响进程,所以采用多线程实现比较好. /// <summary> /// 多线程邮件发送 多线程需注意不要引用到外部方 ...

  4. Delphi XE7编译安卓程序出错了

    昨天编译一个先前可以正常运行的程序,忽然就不能编译了,总是提示这个错误,经过一番排查,终于搞定了,原因:删除了安卓lib引用的JAR和单元文件.如果你也出现这个问题,打开工程全部目录,看一下是否有打小 ...

  5. Problem G: 沉迷字符的WJJ (LCS)

    Contest - 河南省多校连萌(四) Problem G: 沉迷字符的WJJ Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 6  Solved: 5 ...

  6. 【OCP 12c】最新CUUG OCP-071考试题库(62题)

    62.(13-17)choose the best answer: You need to list the employees in DEPARTMENT_ID 30 in a single row ...

  7. kvm快照备份及常用命令

    转载自:http://www.myjishu.com/?p=431 好文章 kvm快照备份及常用命令 kvm快照,分两种: 1种lvm快照,如果分区是lvm,可以利用lvm进行kvm的快照备份 2种由 ...

  8. linux防火墙(五)—— 防火墙的规则备份与还原

    一.第一种备份还原用法,使用工具 iptables-save >/opt/iprules.txt iptables-restore < /opt/iprules.txt #注意导入的文件必 ...

  9. Flink学习笔记-新一代Flink计算引擎

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  10. 基于datax的数据同步平台

    一.需求 由于公司各个部门对业务数据的需求,比如进行数据分析.报表展示等等,且公司没有相应的系统.数据仓库满足这些需求,最原始的办法就是把数据提取出来生成excel表发给各个部门,这个功能已经由脚本转 ...