在 R 中生成規則的數字序列
Jesse John
2023年1月30日
本文將討論在 R 中使用冒號運算子 :
和 seq()
函式生成規則的數字序列。
在 R 中使用冒號運算子生成規則的數字序列
冒號運算子使用格式 from:to
。該序列將從 from
開始,並在 to
或其附近結束。
它遵循以下規則:
- 如果
to
大於from
,則序列將增加 1,否則將減少 1。 - 如果
to
值與前一個值相差接近 1,則將其包含在序列中。文件指出,它可以與 1 相差約1e-7
的數字模糊。
冒號運算子允許我們建立相差 1 的序列。讓我們看一些示例。
示例程式碼:
# Ascending sequence from 1 to 10.
1:10
# Descending sequence from 9 to 0.
9:0
# Ascending sequence from -5.5 to 5.5.
-5.5:5.5
輸出:
> # Ascending sequence from 1 to 10.
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
>
> # Descending sequence from 9 to 0.
> 9:0
[1] 9 8 7 6 5 4 3 2 1 0
>
> # Ascending sequence from -5.5 to 5.5.
> -5.5:5.5
[1] -5.5 -4.5 -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3.5 4.5 5.5
在 R 中使用 seq()
函式生成規則的數字序列
冒號運算子使我們能夠非常快速地製作序列。但我們只能製作數字相差 1 或 -1 的序列。
seq()
函式給了我們更多的控制權。除了 from
和 to
之外,它還有以下引數:
- 我們可以使用
by
引數更改連續數字之間的值。 - 我們可以使用
length.out
或along.with
引數指定我們想要多少個數字。
在示例中,我們將看到以下內容:
- 元素相差一個正值的遞增序列。
- 元素相差負值的遞減序列。
- 在數字之後或之前給定長度的序列,相差一個指定的值。
- 在起點和終點之間等距排列的數字序列。
- 序列與另一個物件一樣長。
示例程式碼:
# Increasing sequence with difference of 5.
seq(from=-10, by=5, to=20)
seq(from=-10, by=5, to=22) # TO value can be approximate.
# Decreasing sequence with difference of -2.
seq(from=10, by=-2, to=2)
seq(from=10, by=-2, to=-10)
# Sequence of 6 numbers after 3 with a difference of 3.
seq(from=3, by=3, length.out=6)
# SEE THE DIFFERENCE BETWEEN THE FOLLOWING TWO.
# Sequence of 5 numbers till 100 with a difference of 10.
seq(to=100, by=10, length.out=5)
# Sequence of 5 numbers till 100 with a difference of -10.
seq(to=100, by=-10, length.out=5)
# Sequence of 6 numbers from 10 to 12.
seq(from=10, to=12, length.out=6)
# Sequence of 10 numbers from 5 to 50.
seq(from=5, to=50, length.out=10)
# Sequence as long as another object.
# First create another object.
vec = rep(c("A", "B", "C"), times=3)
vec # Has 9 elements.
seq(from=100, by=5, along.with=vec)
seq(to=100, by=5, along.with=vec)
輸出:
> # Increasing sequence with difference of 5.
> seq(from=-10, by=5, to=20)
[1] -10 -5 0 5 10 15 20
> seq(from=-10, by=5, to=22) # TO value can be approximate.
[1] -10 -5 0 5 10 15 20
>
> # Decreasing sequence with difference of -2.
> seq(from=10, by=-2, to=2)
[1] 10 8 6 4 2
> seq(from=10, by=-2, to=-10)
[1] 10 8 6 4 2 0 -2 -4 -6 -8 -10
>
> # Sequence of 6 numbers after 3 with difference of 3.
> seq(from=3, by=3, length.out=6)
[1] 3 6 9 12 15 18
>
> # SEE THE DIFFERENCE BETWEEN THE FOLLOWING TWO.
> # Sequence of 5 numbers till 100 with difference of 10.
> seq(to=100, by=10, length.out=5)
[1] 60 70 80 90 100
> # Sequence of 5 numbers till 100 with difference of -10.
> seq(to=100, by=-10, length.out=5)
[1] 140 130 120 110 100
>
> # Sequence of 6 numbers from 10 to 12.
> seq(from=10, to=12, length.out=6)
[1] 10.0 10.4 10.8 11.2 11.6 12.0
> # Sequence of 10 numbers from 5 to 50.
> seq(from=5, to=50, length.out=10)
[1] 5 10 15 20 25 30 35 40 45 50
>
> # Sequence as long as another object.
> # First create another object.
> vec = rep(c("A", "B", "C"), times=3)
> vec # Has 9 elements.
[1] "A" "B" "C" "A" "B" "C" "A" "B" "C"
> seq(from=100, by=5, along.with=vec)
[1] 100 105 110 115 120 125 130 135 140
> seq(to=100, by=5, along.with=vec)
[1] 60 65 70 75 80 85 90 95 100
R 中 seq()
函式的其他用途
我們可以使用 seq()
函式來生成奇數、偶數和數字的倍數的序列。seq()
函式也有助於防止錯誤。
例如,在給出無法使用的引數時,請參閱以下示例中的錯誤訊息。
示例程式碼:
# TO is greater, but BY is negative.
seq(from=1, to=10, by=-1)
# We want a decreasing sequence of 10 numbers with differences of 5 ending at 80.
# The error message tells us that there is a mix-up.
seq(to=80, by=5, length.out=-10)
輸出:
> # TO is greater, but BY is negative.
> seq(from=1, to=10, by=-1)
Error in seq.default(from = 1, to = 10, by = -1) :
wrong sign in 'by' argument
>
> # We want a decreasing sequence of 10 numbers with differences of 5 ending at 80.
> # The error message tells us that there is a mix-up.
> seq(to=80, by=5, length.out=-10)
Error in seq.default(to = 80, by = 5, length.out = -10) :
'length.out' must be a non-negative number
其他序列和模式
seq()
函式還可以生成日期序列。
此外,R 還有許多其他有價值的函式來生成數字或字母的向量。
rep()
函式幫助我們複製向量的元素。這些可以是數字或字串。- 我們可以使用
rnorm()
等函式生成符合理論概率分佈的統計資料。按照其文件中的連結檢視此類功能的完整列表。
示例程式碼:
# Sequence of dates.
seq(from=as.Date("2022-01-31"), by="day", length.out=5)
# Replicated patterns.
# Whole vector repeated.
rep(c("A", "B", "C"), times=4)
# Each element repeated.
rep(c("A", "B", "C"), each=4)
# Repeat elements and the vector.
rep(c("A", "B", "C"), each=2, times=3)
# Generate 6 random values from a normal distribution with mean=5 and sd=2.
rnorm(6, mean=5, sd=2)
輸出:
> # Sequence of dates.
> seq(from=as.Date("2022-01-31"), by="day", length.out=5)
[1] "2022-01-31" "2022-02-01" "2022-02-02" "2022-02-03" "2022-02-04"
>
> # Replicated patterns.
> # Whole vector repeated.
> rep(c("A", "B", "C"), times=4)
[1] "A" "B" "C" "A" "B" "C" "A" "B" "C" "A" "B" "C"
> # Each element repeated.
> rep(c("A", "B", "C"), each=4)
[1] "A" "A" "A" "A" "B" "B" "B" "B" "C" "C" "C" "C"
> # Repeat elements and the vector.
> rep(c("A", "B", "C"), each=2, times=3)
[1] "A" "A" "B" "B" "C" "C" "A" "A" "B" "B" "C" "C" "A" "A" "B" "B" "C" "C"
>
> # Generate 6 random values from a normal distribution with mean=5 and sd=2.
> rnorm(6, mean=5, sd=2)
[1] 4.775072 4.925386 6.758762 3.402821 5.791017 6.864015
作者: Jesse John