R语言数组array函数
数组是一个可以在两个以上的维度存储数据的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函数的更多相关文章
- php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】
本文实例总结了php常用数组array函数.分享给大家供大家参考,具体如下: array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 案例: <?php ...
- R语言:常用函数【转】
数据结构 一.数据管理vector:向量 numeric:数值型向量 logical:逻辑型向量 character:字符型向量list:列表 data.frame:数据框 c:连接为向量或列表len ...
- C语言数组和函数实例练习(二)
1.C语言中不允许函数的嵌套定义,但可以使用函数的嵌套调用. 例1:输入4个整数,找出其中最大的数. #include <stdio.h> #include <stdlib.h> ...
- C语言数组和函数实例练习(一)
C语言的数组和函数部分的知识,在语法上和Java语法是有所相似的,这里只通过实例总结一些自己感觉需要理解的部分知识. 1.数组 数组中的元素具有相同的数据类型:数组一旦创建,不能被改变:数组中元素在内 ...
- R语言 三个函数sort();rank();order()
R语言入门,弄懂了几个简单的函数,分享一下:R语言排序有几个基本函数: sort():rank():order()sort()是对向量进行从小到大的排序rank()返回的是对向量中每个数值对应的秩or ...
- C语言 数组做函数参数退化为指针的技术推演
//数组做函数参数退化为指针的技术推演 #include<stdio.h> #include<stdlib.h> #include<string.h> //一维数组 ...
- C语言数组作为函数参数
数组可以作为函数的参数使用,进行数据传送. 数组用作函数参数有两种形式,一种是把数组元素(下标变量)作为实参使用:另一种是把数组名作为函数的形参和实参使用. 数组元素作函数实参 数组元素就是下标变量, ...
- R语言——基本绘图函数
通过一个综合的例子测试绘图函数 学习的内容是tigerfish老师的教程. 第一节:基本知识 用seq函数产生100位学生的学号. > num = seq(,) > num [] [] [ ...
- R语言do.call 函数用法详解
虽然R语言有类型很丰富的数据结构,但是很多时候数据结构比较复杂,那么基本就会用到list这种结构的数据类型.但是list对象很难以文本的形式导出,因此需要一个函数能快速将复杂的list结构扁平化成da ...
随机推荐
- 调整Linux最大打开文件数
#!/bin/bash ## 文件数限制 ulimit -n ulimit -Sn ulimit -Hn ## fs.nr_open,进程级别 ## fs.file-max,系统级别 ## 最大文件描 ...
- WP8.1StoreApp(WP8.1RT)---发送邮件和短信
在WP7/8中,发送短信是利用了EmailComposeTask和SmsComposeTask来实现的. 在WP8.1 Store App中,原来的方式已经失效,采用了新的方法:ChatMessage ...
- [SSH]struts2-spring-plugin.jar了解
在struts2-spring-plugin.jar中有一个struts-plugin.xml,里面声明了action类由spring工厂创建.在struts2插件文档里,这样写着“The Sprin ...
- 【OCP-12c】2019年CUUG OCP 071考试题库(78题)
78.View the exhibit and examine the structure of the CUSTOMERStable. Which two tasks would require s ...
- “全栈2019”Java异常第十七章:Error详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- “全栈2019”Java第一百零二章:哪些作用域可以声明局部内部类?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Java中 IO类 - File类中的判断功能
package com.hxzy.IOSer;import java.io.*;public class Demo05 { public static void main(String[] args) ...
- [bzoj4009] [HNOI2015]接水果 整体二分+扫描线+dfs序+树状数组
Description 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果. 由于她已经DT FC 了The big black, 她觉得这个游戏太简单了,于是发明了一个更 加 ...
- 基于.NET的开源搜索引擎-DotLucene(2)
NLucene是将 Lucene 从 Java 移植到 .NET 的一个 SourceForge 项目,它从 Lucene 1.2 版本转化而来. Lucene.Net因为 NLucene 项目到20 ...
- Haproxy搭建Web群集
一.Haproxy与LVS LVS不支持正则处理,不能实现动静分离,对于大型网站,LVS的实施配置复杂,维护成本相对较高 Harpoxy是一款可提供高可用性,负载均衡.及基于TCP和HTTP应用的代理 ...