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 ...
随机推荐
- python怎么生成requirements.txt文件
生成文件: pip freeze > requirements.txt 安装依赖: pip install -r requirements.txt
- .Net core 2.0的数据初始化
在StartUp.cs里面,添加Seed方法 public static void Seed(IApplicationBuilder applicationBuilder) { using (var ...
- unity 加载资源
Unity3D中的资源的处理种类 Unity中的资源资源的处理种类大致分为:Resources.StreamingAssets.AssetBundle Resources 是作为一个Unity的保留文 ...
- WPF文字间距
代码: <ItemsControl ItemsSource="{Binding Info}" FontFamily="微软雅黑" FontSize=&qu ...
- 限制html文本框input只能输入数字和小数点
代码: <input type="text" class="txt" name="qty" value="" on ...
- This InfoPath form template is browser-compatible, but it cannot be browser-enabled on the selected site
- all features were running on sitecollection level and at site level But here is the solution, i do ...
- Java-File类获取目录下文件名-遍历目录file.listFiles
package com.hxzy.IOSer;import java.io.*;/*File 类获取功能 * List * ListFile * */public class Demo06 { pub ...
- soapui加载天气预报接口报错Characters larger than 4 bytes are not supported: byte 0xb1 implies a length of more than 4 byte的解决办法
soapui加载天气预报接口时报错如下: Error loading [http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl ...
- WEB基础技术(汇聚页)
WEB基础技术(汇聚页) ------------------------------------------------- WEB WEB概述 HTML CSS JavaScript JavaScr ...
- [科普] 借助 everything 扩展教你屏蔽网址或转发网址
教你屏蔽网址或转发网址 万恶之源 为什么写这篇文章,俺觉得大家应该是有这个需(bai)求(du)的.只是不知道如何操作... 一.屏蔽网址 1.借助系统自带防火墙 (不推荐) Linux 下有 ipt ...