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

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

Here we are explaining how to write an R program to create a vector using: operator and seq() function. Here we are using the built-in function seq for this calculation. The numbers are passed to these functions directly here. To generating regular sequences seq is a standard generic with a default method.

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)).And the argument length.out decides the total length of the sequence and along. with will Outputs a sequence of the same length as the input vector.

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

Given below are the steps which are used in the R program to create a vector using: operator and seq() function. In this R program, we directly give the values to built-in functions. And print the function result. Here we are using variable A for the newly created vector using: operator. And B, C are the newly created vectors using the seq() method.

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-")
## [1] "New vector using : operator-"
print(A)
##  [1]  1  2  3  4  5  6  7  8  9 10
print("New vector using seq() function - ")
## [1] "New vector using seq() function - "
print("Specify step size:")
## [1] "Specify step size:"
B= seq(1, 3, by=0.3)  

print(B)
## [1] 1.0 1.3 1.6 1.9 2.2 2.5 2.8
print("Specify length of the vector:")
## [1] "Specify length of the vector:"
C = seq(1, 5, length.out = 6)

print(C)
## [1] 1.0 1.8 2.6 3.4 4.2 5.0