R 中 seq() 和 sequence() 之间的区别是什么?

roc*_*occ -1 r sequence seq

我是 R 新手,想知道seq()和之间有什么区别sequence()。两者都输入了R不同的结果:

> seq(c(10,5))
[1] 1 2
> sequence(c(10,5))
 [1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5
Run Code Online (Sandbox Code Playgroud)

谁能帮我这个?

ial*_*alm 6

如果您有问题,一个好的起点是使用help()or?命令查看帮助文件。

如果您查看 中的帮助文件?seq,它会说它通常需要一些参数:

## Default S3 method:
seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
    length.out = NULL, along.with = NULL, ...)
Run Code Online (Sandbox Code Playgroud)

因此,如果您执行类似seq(from = 1, to = 10)or 的操作seq(1, 10),它将为您提供一个从 1 到 10 的向量:

seq(1, 10)
[1] 1  2  3  4  5  6  7  8  9  10
Run Code Online (Sandbox Code Playgroud)

您的使用情况seq()并不多见,但该行为记录在帮助文件的下方

[ seq(from)] 生成序列 1, 2, ..., length(from)(就像along.with已指定参数一样),除非参数是长度为 1 [...] 的数字

因此,seq(c(1, 10))将输出一个“1, 2”序列,因为它给出的唯一参数是长度为 2 的向量。

的行为sequence()更直接,并在帮助文件(从 访问?sequence)中进行了简要说明。此外,帮助文件底部显示了行为示例。

sequence(c(3, 2)) # the concatenated sequences 1:3 and 1:2.
#> [1] 1 2 3 1 2
Run Code Online (Sandbox Code Playgroud)

欢迎来到奇妙而又古怪的 R 编程世界。我建议你先学习 R 语言的基础知识,在问基本问题之前先查看帮助文件和文档,保留 stackoverflow 作为解决更困难的编程问题的资源.