R – Create Vector
To create a vector in R programming, there are three ways: c() function, seq() function and :
operator.
In this tutorial, we will learn how to create a vector using the the above mentioned ways.
Create Vector using c() Function
c()
function combines values together. We can use this c()
function to combine the given values and create a vector.
In the following R program, we will use c()
function to create a vector of double values.
example.R
x <- c(1, 4, 9, 16, 25)
cat(x)
Output
1 4 9 16 25
Create Vector using seq() Function
seq()
function creates a sequence of values. We can use this seq()
function to create a vector with sequence values.
In the following R program, we will use seq()
function to create a vector of double values.
example.R
x <- seq(0, 20, length.out=5)
cat(x)
Output
0 5 10 15 20
Create Vector using :
Operator
:
creates continuous values. We can use this :
operator to create a vector with continuous values.
In the following R program, we will use :
operator to create a vector.
example.R
x <- 1:5
cat(x)
Output
1 2 3 4 5
Conclusion
In this R Tutorial, we learned how to create a vector in R language, using different functions like c()
, seq()
; and operators like :
, with the help of examples.