Remove NA Columns from xts Time Series in R (2 Examples)

 

In this tutorial, I’ll illustrate how to drop NA columns from an xts object in R programming.

The article will contain this information:

Let’s dive into it!

 

Exemplifying Data & Packages

Initially, I have to create some example data:

data <- data.frame(date = as.Date(c("2022-10-11",                      # Create example data
                                    "2023-12-12",
                                    "2019-07-15",
                                    "2018-06-03",
                                    "2023-01-17",
                                    "2025-10-06")),
                   x1 = 1:6,
                   x2 = c(1:5, NA),
                   x3 = NA)
data                                                                   # Print example data

 

table 1 data frame remove na columns from xts time series r

 

As you can see based on Table 1, our example data is a data frame and contains six rows and four variables.

The first variable contains dates and the other variables contain different values. Some of the columns contain NA values (i.e. missing data).

In order to use the functions of the xts package, we also have to install and load xts:

install.packages("xts")                                                # Install & load xts
library("xts")

Now, we can convert our data frame to an xts time series object as shown below:

data_xts <- xts(data[ , colnames(data) != "date"], data$date)          # Convert data frame to xts object
data_xts                                                               # Print xts object
#            x1 x2 x3
# 2018-06-03  4  4 NA
# 2019-07-15  3  3 NA
# 2022-10-11  1  1 NA
# 2023-01-17  5  5 NA
# 2023-12-12  2  2 NA
# 2025-10-06  6 NA NA

The previous R syntax has created an xts object called data_xts.

 

Example 1: Remove Columns of xts Object that Contain Only NA Values

In this section, I’ll demonstrate how to drop all variables from our xts object that contain only NA values.

for this task, we can use the colSums, is.na, and nrow functions as shown below:

data_xts_1 <- data_xts[ , colSums(is.na(data_xts)) != nrow(data_xts)]  # Remove all-NA columns
data_xts_1                                                             # Print xts subset
#            x1 x2
# 2018-06-03  4  4
# 2019-07-15  3  3
# 2022-10-11  1  1
# 2023-01-17  5  5
# 2023-12-12  2  2
# 2025-10-06  6 NA

Have a look at the previous output of the RStudio console. As you can see, we have removed the all-NA variable x3 from our time series data.

However, you can also see that the column x2 has been kept, even though it also contains an NA value. In case you want to exclude this column as well, keep on reading!

 

Example 2: Remove Columns of xts Object that Contain At Least One NA Value

This example shows how to delete all columns from an xts object that contain one or more NA values.

To accomplish this, we can use the colSums and is.na function as shown in the following R code:

data_xts_2 <- data_xts[ , colSums(is.na(data_xts)) == 0]               # Remove columns with at least one NA
data_xts_2                                                             # Print xts subset
#            x1
# 2018-06-03  4
# 2019-07-15  3
# 2022-10-11  1
# 2023-01-17  5
# 2023-12-12  2
# 2025-10-06  6

Only the variable x1 has been kept. All the other columns have been removed.

 

Video & Further Resources

Do you need more information on the examples of this tutorial? Then you might have a look at the following video on my YouTube channel. In the video, I show the content of this tutorial in RStudio.

 

The YouTube video will be added soon.

 

Furthermore, you might want to have a look at the other tutorials on my homepage:

 

At this point you should know how to delete NA columns from an xts time series in the R programming language. In case you have further comments or questions, please let me know in the comments. In addition, please subscribe to my email newsletter to receive updates on the newest articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top