R语言seq()函数的调用方法

来自:网络
时间:2021-03-11
阅读:

看到有很多读者浏览了这篇文章,心里很是开心,为了能够更好地帮助大家,决定再修改一下,帮助大家更好地理解。

--------修改于:2018年4月28日

为了方便大家在开发环境中直接实验测试代码,下面,我将说明和函数的用法全部用英文给出(避免乱码),并加以注释,希望能够对大家有所帮助!

首先,我们来看一个seq()函数应用的实例!

x <- seq(0, 10, by = 0.01)
y <- sin(x)
plot(y)

下面,我们来看函数的主要使用方法!

注意:在本文调用函数时,均采用写出入口参数名的方法,比如:

seq(from = 1, to = 2)

这样函数的调用更加清晰,在调用较多函数时,不会发生混乱和参数匹配错误。

方式一:seq(from, to)

from:生成向量的起点,to:生成向量的终点,默认步长为1(可修改)

a <- seq(from = 1, to = 2)
# [1, 2]

方式二:seq(from, to, by = )

by:向量元素之间的步长

a <- seq(from = 1, to = 3, by = 0.5)
# [1, 1.5, 2, 2.5, 3]

方式三:seq(from, to, length.out = )

length.out:向量中元素数目

a <- seq(from = 1, to = 3, length.out = 5)
# [1, 1.5, 2, 2.5, 3]

方式四:seq(along.with = )

along.with:表示生成的向量为现有一向量元素的索引

x <- c(1.2, 5.2, 6.3, 4.6)
a <- seq(along.with = x)
# [1, 2, 3, 4]

方式五:seq(from)

该方式和方式四功能相似

x <- c(1.2, 5.2, 6.3, 4.6)
a <- seq(from = x)
# [1, 2, 3, 4]

方式6:seq(length.out = )

生成从1开始,步长为1,长度为length.out的向量

a <- seq(length.out = 5)
# [1, 2, 3, 4, 5]

上述几种方式为较为常见的方式,详细的函数说明如下:

Sequence Generation

Description

Generate regular sequences. seq is a standard generic with a default method. seq.int is a primitive which can be much faster but has a few restrictions. seq_along and seq_len are very fast primitives for two common cases.

---------------------------------------------

Usage

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

seq_len(length.out)

---------------------------------------------

Arguments

1:...

arguments passed to or from methods.

2:from, to

the starting and (maximal) end values of the sequence. Of length 1 unless just from is supplied as an unnamed argument.

3:by

number: increment of the sequence.

4:length.out

desired length of the sequence. A non-negative number, which for seq and seq.int will be rounded up if fractional.

5:along.with

take the length from the length of this argument.

参考:https://blog.csdn.net/jiluben/article/details/40024607

返回顶部
顶部