在 R 中生成规则的数字序列

Jesse John 2023年1月30日 2022年5月18日
  1. 在 R 中使用冒号运算符生成规则的数字序列
  2. 在 R 中使用 seq() 函数生成规则的数字序列
在 R 中生成规则的数字序列

本文将讨论在 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() 函数给了我们更多的控制权。除了 fromto 之外,它还有以下参数:

  • 我们可以使用 by 参数更改连续数字之间的值。
  • 我们可以使用 length.outalong.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
Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.