Creating a Vector of sequenced elements in R Programming – seq() Function

Creating a Vector of sequenced elements in detail

Sequenced elements refer to items arranged in a specific order, one after the other. This concept is commonly applied in various fields to organize data systematically.

seq() Function: The seq() function in R Programming Language is used to generate a sequence of elements in a vector. This function allows customization of the sequence using optional arguments such as the starting point, ending point, step size, and desired length of the vector.

Syntax:

seq(from, to, by, length.out)

Parameters:

  • from: Starting value of the sequence
  • to: Ending value of the sequence
  • by: Step size or the difference between consecutive elements
  • length.out: Desired length of the sequence
Creating a Vector of Sequenced Elements using seq() Function

1. Basic Sequence Creation

# Create a sequence of elements
sequence <- seq(1, 5)
sequence

Output:

[1] 1 2 3 4 5

2. Using by Argument

# Creating a vector with specified step size
sequence_by <- seq(2, 12, by = 3)
sequence_by

Output:

[1]  2  5  8 11

3. Using from and to Arguments

# Creating a sequence using from and to arguments
sequence_from_to <- seq(from = 15, to = 20)
sequence_from_to

sequence_from_to
# Creating a sequence using from and to arguments
sequence_from_to <- seq(from = 15, to = 20)
sequence_from_to

Output:

[1] 15 16 17 18 19 20

4. Using length.out Argument

# Creating a sequence with specified length
sequence_length_out <- seq(from = 5, to = 15, length.out = 4)
sequence_length_out

Output:

[1]  5.00  8.33 11.67 15.00

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *