R Basic

Creating an Empty List in R

Here are three ways to create an empty list in R:

  1. Using list()
  2. Using vector()
  3. Assigning NULL to empty an existing list

An empty list means it does not contain any elements.

An empty list and NULL have a key difference. An empty list means there is a list object with 0 elements, and a NULL list means it is NULL; it does not have a class of “list”.

Why would you create an empty list?

The most frequent use is to store elements that you will generate in the future, as you start accumulating them. It provides a flexible way to dynamically create a data structure.

You can use an empty list as a container, placeholder, or starting point for more complex data structures.

Method 1: Using list()

The list() function creates a list object with 0 elements. Don’t pass any arguments to this function.

Syntax

empty_data = list()

If you find the length of the list, it will be 0.

Example

empty_list <- list() 

print(empty_list)

print(length(empty_list))

Output

list()
[1] 0

When you want to pre-allocate a list of specific lengths, use this approach. It also makes it more efficient because you know in advance how many elements a list will hold eventually.

Creating a list of empty lists

While working on a project, you might come across a requirement when you want a single container to hold multiple independent lists.

num_lists <- 5 # Number of empty lists you want
list_of_lists <- replicate(num_lists, list())

# Verify:

length(list_of_lists) # Output: 5

class(list_of_lists) # Output: "list"

class(list_of_lists[[1]]) # Output: "list" (each element is a list)

Method 2: Using vector()

The vector(“list“, length = 0) function is a more flexible approach to creating an empty list with a specific length. For example, you can have a list with five NULL values, which is itself empty.

Visual representation

Here, the length is 5. Even though it is empty.

Syntax

vector(mode = 'list', length)

Parameters

Name Description
mode It must be a list in our context.
length It is several elements filled with NULL.

Example

len <- 5

empty_list <- vector(mode = "list", length = len)

empty_list

class(empty_list)

print(length(empty_list))

Output

[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
NULL

[1] "list"

[1] 5

Method 3: Assigning NULL to an existing list

If you assign NULL to an existing list, it will become NULL. It’s crucial to know that after assigning NULL, the class of the object becomes “NULL”, not “list”.

So, here, the class itself is changing from “list” to NULL. If you want to keep the class as a “list”, then refer to the list() approach.

Example

rv <- c(1, 2, 3, 4)

lst <- list(rv)
lst
typeof(lst)
lst <<- NULL
lst
typeof(lst)

Output

[[1]]
[1] 1 2 3 4

[1] "list"
NULL
[1] "NULL"

That’s it!

Recent Posts

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

5 days ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

5 days ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

2 weeks ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

3 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

1 month ago