Skip to content
Related Articles

Related Articles

How to create an empty vector in R ?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 21 Apr, 2021
View Discussion
Improve Article
Save Article

In this article, we are going to see how to create an empty vector in R Programming Language. There are five ways of creating an empty vector and each of them will be discussed in detail below:

  • Using c()
  • Using vector()
  • Creating empty vectors using NULL
  • Using numeric() method
  • Using rep() method

Method 1: Using c()

Here in this, we need not pass anything while creating an empty vector as we are creating empty vector.

Syntax:

vector name <- c()

Where, vector name can be any valid identifier.

Example :

R




# Here we are creating empty
# vector using c().
vect <- c()
  
vect

Output:

NULL

Output is null as we are not passing any data to it.

Method 2: Using vector()

Here in this, we need not pass anything as we are creating an empty vector:

Syntax:

vector name  <- vector()

Where, vector name can be any valid identifier.

Example

R




# Here we are creating empty vector
# using vector().
vect <- vector()
  
vect

Output:

logical(0)

Output is empty as we are not passing any data.

Method 3: Creating empty vectors using NULL

 Here we are creating empty vector by passing null.

Syntax:

 vector name  <- NULL

Where, vector name can be any valid identifier.

Example 

R




# Here we are creating empty 
# vector using NULL
a<- NULL
  
a

Output:

NULL

Method 4:Using numeric() method

Here we are creating empty vector by using numeric().

Syntax:

vector name <- numeric()

Where, vector name can be any valid identifier.

Example

R




# creating empty vector vec using 
# numeric function.
vec <- numeric()
  
vec

Output:

numeric(0)

Method 5:Using rep() method

Here we can directly use rep() to create empty vector.

Syntax:

vector name<- rep()

where, vector name can be any valid identifier.

Example:

R




# creating empty vector vec using
# numeric function.
vec <- rep()
  
vec

Output:

NULL


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!