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

What is the defference between seq() and sequence() in R?

我是 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

谁能帮我解决这个问题?

如果您有任何疑问,一个好的开始是使用 help()? 命令查看帮助文件。

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

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

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

seq(1, 10)
[1] 1  2  3  4  5  6  7  8  9  10

您对 seq() 的用法不多,但在帮助文件的下方记录了该行为

[seq(from)] generates the sequence 1, 2, ..., length(from) (as if argument along.with had been specified), unless the argument is numeric of length 1 [...]

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

sequence() 的行为更 straight-forward,并且在帮助文件(从 ?sequence 访问)中有简洁的解释。此外,帮助文件底部显示了行为示例。

sequence(c(3, 2)) # the concatenated sequences 1:3 and 1:2.
#> [1] 1 2 3 1 2

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