R語言seq()函數的調用方法

看到有很多讀者瀏覽瞭這篇文章,心裡很是開心,為瞭能夠更好地幫助大傢,決定再修改一下,幫助大傢更好地理解。

——–修改於: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

到此這篇關於R語言seq()函數的調用方法的文章就介紹到這瞭,更多相關R語言seq()函數內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: