R語言seq()函數(shù)的調用方法
看到有很多讀者瀏覽了這篇文章,心里很是開心,為了能夠更好地幫助大家,決定再修改一下,幫助大家更好地理解。
--------修改于:2018年4月28日
為了方便大家在開發(fā)環(huán)境中直接實驗測試代碼,下面,我將說明和函數(shù)的用法全部用英文給出(避免亂碼),并加以注釋,希望能夠對大家有所幫助!
首先,我們來看一個seq()函數(shù)應用的實例!
x <- seq(0, 10, by = 0.01) y <- sin(x) plot(y)
下面,我們來看函數(shù)的主要使用方法!
注意:在本文調用函數(shù)時,均采用寫出入口參數(shù)名的方法,比如:
seq(from = 1, to = 2)
這樣函數(shù)的調用更加清晰,在調用較多函數(shù)時,不會發(fā)生混亂和參數(shù)匹配錯誤。
方式一: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:向量中元素數(shù)目
a <- seq(from = 1, to = 3, length.out = 5) # [1, 1.5, 2, 2.5, 3]
方式四:seq(along.with = )
along.with:表示生成的向量為現(xiàn)有一向量元素的索引
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]
上述幾種方式為較為常見的方式,詳細的函數(shù)說明如下:
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()函數(shù)的調用方法的文章就介紹到這了,更多相關R語言seq()函數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
R語言利用loess如何去除某個變量對數(shù)據(jù)的影響詳解
這篇文章主要給大家介紹了關于R語言利用loess去除某個變量對數(shù)據(jù)的影響的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11

