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 [更多]
显示目录

向量



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

点击购买 固件广场

R语言 向量

向量是最基本的R语言数据对象,有六种类型的原子向量。 它们是逻辑,整数,双精度,复杂,字符和原始。

创建向量

单元素向量

即使在R语言中只写入一个值,它也将成为长度为1的向量,并且属于上述向量类型之一。

# Atomic vector of type character.
print("abc");

# Atomic vector of type double.
print(12.5)

# Atomic vector of type integer.
print(63L)

# Atomic vector of type logical.
print(TRUE)

# Atomic vector of type complex.
print(2+3i)

# Atomic vector of type raw.
print(charToRaw('hello'))

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

[1] "abc"
[1] 12.5
[1] 63
[1] TRUE
[1] 2+3i
[1] 68 65 6c 6c 6f
`

多元素向量

对数值数据使用冒号运算符

# Creating a sequence from 5 to 13.
v <- 5:13
print(v)

# Creating a sequence from 6.6 to 12.6.
v <- 6.6:12.6
print(v)

# If the final element specified does not belong to the sequence then it is discarded.
v <- 3.8:11.4
print(v)

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

[1]  5  6  7  8  9 10 11 12 13
[1]  6.6  7.6  8.6  9.6 10.6 11.6 12.6
[1]  3.8  4.8  5.8  6.8  7.8  8.8  9.8 10.8

使用sequence (Seq.)序列运算符

# Create vector with elements from 5 to 9 incrementing by 0.4.
print(seq(5, 9, by = 0.4))

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

[1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0

使用C()函数

如果其中一个元素是字符,则非字符值被强制转换为字符类型。

# The logical and numeric values are converted to characters.
s <- c('apple','red',5,TRUE)
print(s)

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

[1] "apple" "red"   "5"     "TRUE"

访问向量元素

使用索引访问向量的元素。 []括号用于建立索引。 索引从位置1开始。在索引中给出负值会丢弃来自result.TRUE,FALSE或0和1的元素,也可用于索引。

# Accessing vector elements using position.
t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
u <- t[c(2,3,6)]
print(u)

# Accessing vector elements using logical indexing.
v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
print(v)

# Accessing vector elements using negative indexing.
x <- t[c(-2,-5)]
print(x)

# Accessing vector elements using 0/1 indexing.
y <- t[c(0,0,0,0,0,0,1)]
print(y)

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

[1] "Mon" "Tue" "Fri"
[1] "Sun" "Fri"
[1] "Sun" "Tue" "Wed" "Fri" "Sat"
[1] "Sun"

向量操作

向量运算

可以添加,减去,相乘或相除两个相同长度的向量,将结果作为向量输出。

# Create two vectors.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2)

# Vector addition.
add.result <- v1+v2
print(add.result)

# Vector substraction.
sub.result <- v1-v2
print(sub.result)

# Vector multiplication.
multi.result <- v1*v2
print(multi.result)

# Vector division.
divi.result <- v1/v2
print(divi.result)

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

[1]  7 19  4 13  1 13
[1] -1 -3  4 -3 -1  9
[1] 12 88  0 40  0 22
[1] 0.7500000 0.7272727       Inf 0.6250000 0.0000000 5.5000000

向量元素回收

如果我们对不等长的两个向量应用算术运算,则较短向量的元素被循环以完成操作。

v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11)

add.result <- v1+v2
print(add.result)

sub.result <- v1-v2
print(sub.result)

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

[1]  7 19  8 16  4 22
[1] -1 -3  0 -6 -4  0

向量元素排序

向量中的元素可以使用sort()函数排序。

v <- c(3,8,4,5,0,11, -9, 304)

# Sort the elements of the vector.
sort.result <- sort(v)
print(sort.result)

# Sort the elements in the reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

# Sorting character vectors.
v <- c("Red","Blue","yellow","violet")
sort.result <- sort(v)
print(sort.result)

# Sorting character vectors in reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

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

[1]  -9   0   3   4   5   8  11 304
[1] 304  11   8   5   4   3   0  -9
[1] "Blue"   "Red"    "violet" "yellow"
[1] "yellow" "violet" "Red"    "Blue"
由JSRUN为你提供的R在线运行、在线编译工具
        JSRUN提供的R 在线运行,R 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout