R在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
尝试说明图片 发布于:2024-04-10 13:37 --- 比较公式和循环计算平均值的流逝时间 发布于:2024-03-10 19:24 农业R语言统计 发布于:2023-12-14 14:02 朱兴垚-202105002605 发布于:2023-12-03 23:45 R demo 发布于:2023-10-06 16:14 小姑父的箱线图 发布于:2023-07-28 17:33 第一章第二章 发布于:2023-06-12 11:01 R入门预备知识 发布于:2023-06-09 15:04 创建一个数轴 发布于:2023-06-07 15:48 生成随机数 发布于:2023-05-10 17:10 读取gct文件 发布于:2023-05-06 11:08 excel画图 发布于:2023-02-09 18:17 储存经纬度的数组 发布于:2023-01-28 16:20 计算,logp函数,修改 发布于:2022-11-15 16:41 用 Monte Carlo 方法进行 概率和分位计算 发布于:2022-11-01 09:49 haoyong hho 发布于:2022-10-25 20:36 数理统计大作业代码 发布于:2022-10-24 16:44 画图——df=12的t分布 发布于:2022-10-23 19:18 统计作图题 发布于:2022-10-24 18:39 统计分析题 发布于:2022-10-23 16:39 ISYE 6501 HW8 (11.1) 发布于:2022-10-17 10:48 城市广告市场案例 发布于:2022-10-07 15:32 画sin()函数图像 发布于:2022-08-24 17:10 我的测试代码 发布于:2022-08-10 10:23 离散卷积逆 发布于:2022-06-20 17:03 定积分直接求和 发布于:2022-06-20 03:29 Buffon's needle problem 发布于:2022-06-03 12:48 试运行输出 发布于:2022-05-06 08:54 计算向量中的两两差值 发布于:2022-05-02 16:52 30日晚上10:00 发布于:2022-05-01 10:38 毕业论文代码 发布于:2022-03-06 23:18 中级计量模拟实验1 发布于:2021-11-18 14:15 第一个demo,不知道啥环境。 发布于:2021-11-09 09:18 R语言项目实验 发布于:2021-09-27 17:02 矩阵中的公式 发布于:2021-04-22 10:55 方块地图骰子判定 发布于:2021-01-10 17:24 R语言Hello World 发布于:2020-08-23 14:07 R语言Hello World 发布于:2020-08-23 14:07 R语言Hello World 发布于:2020-08-04 10:53 [更多]
显示目录

R语言 数组



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

R语言 数组

数组是可以在两个以上维度中存储数据的R数据对象。 例如 - 如果我们创建一个维度(2,3,4)的数组,则它创建4个矩形矩阵,每个矩阵具有2行和3列。 数组只能存储数据类型。
使用array()函数创建数组。 它使用向量作为输入,并使用dim参数中的值创建数组。

例子

以下示例创建一个由两个3x3矩阵组成的数组,每个矩阵具有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(row.names,column.names,
   matrix.names))
print(result)

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

, , Matrix1

     COL1 COL2 COL3
ROW1    5   10   13
ROW2    9   11   14
ROW3    3   12   15

, , Matrix2

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

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

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

由JSRUN为你提供的R在线运行、在线编译工具
        JSRUN提供的R 在线运行,R 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout