Creating a Vector of sequenced elements in R Programming – seq() Function
seq() function in R Language is used to create a sequence of elements in a Vector. It takes the length and difference between values as optional argument.
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
Example 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 vectorsprint(vec1)print(vec2) |
Output:
[1] 1 3 5 7 9 [1] 1.0 2.5 4.0 5.5 7.0 8.5 10.0
Example 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 vectorsprint(vec1)print(vec2) |
Output:
[1] 10 8 6 4 2 [1] 10 7 4 1


