R Program to create a vector using : operator and seq() function


February 8, 2023, Learn eTutorial
1203

How to create a vector using: the operator and seq() function using the R program?

To write an R program to create a vector using ':' operator and 'seq()' function, we have to use the built-in function seq(). The seq() function in R can able to generate a series or sequence of values with the limit we are providing. The limit numbers are passed to these functions directly here. Syntax of the seq() is

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

Where, 

  • from and to are the beginning and terminating the number of the sequence
  • by is the increment of the given sequence it is calculated as ((to-from) /(length.out-1)).
  • argument length.out decides the total length of the sequence
  • along. with will Output a sequence of the same length as the input vector.

In this R program, we directly give the values to the built-in function and print the return value of the function. Here we are using variable A for the new vector created using ':' operator. B, and C are the newly created vectors using the seq() method. we take the user input for B and C vector sizes.

ALGORITHM

STEP 1: Use the operator: for new vector creation as A=1:10

STEP 2: Print the vector A

STEP 3: Use the method seq() for new vector creation as B= seq(1, 3, by=0.3) 

STEP 4: Print the vector B

STEP 5: Use the method seq() for new vector creation as C = seq(1, 5, length.out = 6) 

STEP 6: Print the vector C

R Source Code

                                          A = 1:10
print("New vector using : operator-")
print(A)
print("New vector using seq() function-")
print("Specify step size:")
B= seq(1, 3, by=0.3)  
print(B)
print("Specify length of the vector:")
C = seq(1, 5, length.out = 6)
print(C)

                                      

OUTPUT

[1] "New vector using : operator-"
[1]  1  2  3  4  5  6  7  8  9 10 
[1] "New vector using seq() function-"
[1] "Specify step size:"
[1] 1.0 1.3 1.6 1.9 2.2 2.5 2.8
[1] "Specify length of the vector:"
[1] 1.0 1.8 2.6 3.4 4.2 5.0