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)
当我们上面的代码执行时,它产生以下结果:
, , Matrix1ROW1 ROW2 ROW3 COL1 5 10 13 COL2 9 11 14 COL3 3 12 15, , Matrix2ROW1 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] 13ROW1 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
转载于:https://www.cnblogs.com/csguo/p/7294328.html
R语言数组array函数相关推荐
- C语言数组测试函数,(完整word版)C语言数组与函数阶段测试题答案---(20200807070313)(8页)-原创力文档...
} } C语言数组与函数阶段测试题答案 一.选择题( 18*2=36 分) 1. 在定义" int a[5][6]; "后,第 11个元素是 [ C ] A.a[2][5] B . ...
- PHP常用数组(Array)函数整理
PHP常用数组(Array)函数整理 整理了一份PHP开发中数组操作大全,包含有数组操作的基本函数.数组的分段和填充.数组与栈.数组与列队.回调函数.排序.计算.其他的数组函数等. 一.数组操作的基本 ...
- oracle矩阵函数,R语言矩阵matrix函数
矩阵是元素布置成二维矩形布局的R对象. 它们包含相同原子类型的元素.尽管我们可以创建只包含字符或只逻辑值的矩阵,但是它们没有多大用处.我们使用的是在数学计算中含有数字元素矩阵. 使用 matrix() ...
- 「杂谈」如何写好R语言apply家族函数
感觉好久没发有关R语言的博客了.这一年来,我着手开发了两个R包,但基本没怎么发博客. 原因也很简单,可能是我个人的私心吧,或者说是典型的"理论研究者"的"通病" ...
- R语言使用basename函数获取数据链接地址中的文件名称(removes all of the path up to and including the last path separator )
R语言使用basename函数获取数据链接地址中的文件名称(removes all of the path up to and including the last path separator (i ...
- R语言使用unzip函数解压压缩文件(Extract or List Zip Archives)
R语言使用unzip函数解压压缩文件(Extract or List Zip Archives) 目录 R语言使用unzip函数解压压缩文件(Extract or List Zip Archives) ...
- R语言使用across函数一次性将多个数据列进行离散化(categorize):或者pivot_longer函数转化为长表、对转化为长表的数值数据列进行离散化、pivot_wider将数据转化为宽表
R语言使用across函数一次性将多个数据列进行离散化(categorize):或者pivot_longer函数转化为长表.对转化为长表的数值数据列进行离散化.pivot_wider将数据转化为宽表 ...
- R语言使用aov函数进行双因素方差分析(Two-way factorial ANOVA)、使用HH包中的interaction2wt函数为任何阶的双因素方差分析可视化主效应和交互作用图、箱图显示主效应
R语言使用aov函数进行双因素方差分析(Two-way factorial ANOVA).使用HH包中的interaction2wt函数为任何阶的双因素方差分析可视化主效应和交互作用图(Main ef ...
- R语言使用trimws函数:trimws函数去除(删除、remove)字符串头尾的空格
R语言使用trimws函数:trimws函数去除(删除.remove)字符串头尾的空格 目录 R语言使用trimws函数:trimws函数去除(删除.remove)字符串头尾的空格
最新文章
- Rabbitmq基本框架和安装(1)
- Quartz.net 定时任务矿建Demo
- Android动态获取权限
- 如何在鼠标hover时改变标注的样式
- JQuery 基础 || 目前 jQuery 有三个大版本||JQuery快速入门
- wkWebView 的一些问题
- python爬虫资源大全_Python爬虫抓取纯静态网站及其资源(基础篇)
- 【渝粤题库】广东开放大学 岭南文化概论专科 形成性考核
- vector的应用练习
- erp生产管理系统流程_仁和ERP生产制造业ERP管理系统库存管理
- 浅谈Hybrid技术的设计与实现第二弹
- 判断显卡好坏的步骤、新买显卡装机后不亮解决
- 如何优雅地编码文本中的位置信息?三种positioanl encoding方法简述
- IS技术:稳定压倒一切
- ADOBE CS3 序列号
- 虚幻学习4---制作实时逼真的毛发【笔记】
- 超详细如何配置NAT
- WPA3 vs WPA2
- 平面设计零基础怎么设计出一个完美的名片
- F5 BIG-IP LTM 负载均衡器 功能介绍 下篇
热门文章
- 微信小程序隐藏菜单栏目下转发按钮的几个方法
- php 云端桌面,开源云桌面程序DZZ v1.2新版功能预览
- Python笔记-UiSelector中resourceId定位方式
- SQL笔记-通过构建索引表方便数据库管理
- 前端笔记-vue cli为web添加底纹
- 6.4.3树和森林的遍历
- 2015广州计算机网络试题及答案,2015广州大学计算机网络试卷-B卷及答案.doc
- php关闭gd库,详解php开启gd库的两种方法
- android手机评测,Android手机 三大浏览器对比评测
- pe常用软件_验证几款U盘PE系统,找出来纯净的几个请大家参考