在 R 编程中创建序列元素的向量 – seq()函数
R 语言中的seq()
函数用于在 Vector 中创建元素序列。它将值之间的长度和差异作为可选参数。
Syntax:
seq(from, to, by, length.out)
Parameters:
from: Starting element of the sequence
to: Ending element of the sequence
by: Difference between the elements
length.out: Maximum length of the vector
示例 1:
# R Program to illustrate
# the use of seq() Function
# Creating vector using seq()
vec1 <- seq(1, 10, by = 2)
vec2 <- seq(1, 10, length.out = 7)
# Printing vectors
print(vec1)
print(vec2)
输出:
[1] 1 3 5 7 9
[1] 1.0 2.5 4.0 5.5 7.0 8.5 10.0
示例 2:
# R Program to illustrate
# the use of seq() Function
# Creating vector using seq()
vec1 <- seq(10, 1, by = -2)
vec2 <- seq(10, 1, length.out = 4)
# Printing vectors
print(vec1)
print(vec2)
输出:
[1] 10 8 6 4 2
[1] 10 7 4 1