Creating a Vector of sequenced elements in R Programming – seq() Function
In This article, we will discuss how we Create a Vector of sequenced elements in R Programming Language using seq() Function.
What are sequenced elements?
Sequenced elements mean things that are placed in a particular order, one after another. This concept is often used in various fields.
seq() Function
seq()
function in R Programming Language is used to create a sequence of elements in a Vector. It takes the length and difference between values as an 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
Creating a Vector of sequenced using seq() Function
R
#Create sequence elements seq (1:10) |
Output:
[1] 1 2 3 4 5 6 7 8 9 10
Creating a Vector of sequenced using by arguments
R
# R Program to illustrate # the use of seq() Function # Creating vector using seq() vec1 <- seq (1, 10, by = 2) vec1 |
Output:
[1] 1 3 5 7 9
Creating a Vector of sequenced using from and to arguments
R
# R Program to illustrate # the use of seq() Function # Creating vector using seq() vec1 <- seq (from=22,to=44) vec1 |
Output:
[1] 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
Seq() function with argument length.out
R
# Using seq() with length.out argument result_vector <- seq (from = 1, to = 10, length.out = 5) print (result_vector) |
Output:
[1] 1.00 3.25 5.50 7.75 10.00
The length.out
argument is set to 5, meaning that R will create a sequence of length 5 starting from 1 to 10. The result will be a sequence evenly spaced between 1 and 10 with a length of 5.