vector()Function to Create an Empty Vector in R
numeric()Function to Create a Numeric Empty Vector in R
A Vector is one of the fundamental data structures in R. It is used to store elements in a sequence, but unlike lists, all elements in a vector must be of the same data type. Based on the type of data stored in a vector, we can classify it into Numerical, Logical, Integer, DateTime, Factors, and Character Vectors.
In R programming, we can create a Vector using a few built-in functions.
vector() Function to Create an Empty Vector in R
vector() function in R is used to create a vector of the specified length and type specified by
mode by default is logical but can be changed to numeric, character, or even to list or some expression.
An example to create an empty Vector is shown below:
vec <- vector()
numeric() Function to Create a Numeric Empty Vector in R
We can use the
numeric() function to create Numeric Vectors in R. We can also pass the length of the vector as its argument.
An example to create an empty Numeric Vector is shown below:
vec2 <- numeric()
Similarly, we can also use the
logical() function to create empty vectors of Character and Logical type and specify its length using the
length argument. We can check whether an object is a vector or not using
is.vector() function, which returns
TRUE for vector objects, as shown below:
> is.vector(vec2)  TRUE