How to Remove Null Elements from a List in R

04.08.2021

Often when working with data, we will find NULL or missing values. There are many ways to work with these, but one simple, and not always the best, way is to remove these values. In this article we will see how to remove NULL values from a list in R.

To remove NULL elements from a list, we can use the compact function from the tidyverse package. First we import that library, then we call the compact function on our list. this will return a new list without the NULL values.

library(tidyverse)

bad.list = list(1, NULL, 3)

good.list = compact(bad.list)

print(good.list)