Add New Data Frame Rows for Missing Dates in R (Example Code)

This tutorial illustrates how to add new rows in case a date is missing in the R programming language.

Example Data

my_df <- data.frame(time = as.Date(c("2021-05-10",  # Create example data
                                     "2021-05-13",
                                     "2021-05-15")),
                    x1 = 1:3,
                    x2 = letters[1:3])
my_df                                               # Print example data
#         time x1 x2
# 1 2021-05-10  1  a
# 2 2021-05-13  2  b
# 3 2021-05-15  3  c

Example: Add New Rows in Case of Missing Dates Using padr Package

install.packages("padr")                            # Install & load padr
library("padr")
pad(my_df)                                          # Using pad() function
#         time x1   x2
# 1 2021-05-10  1    a
# 2 2021-05-11 NA <NA>
# 3 2021-05-12 NA <NA>
# 4 2021-05-13  2    b
# 5 2021-05-14 NA <NA>
# 6 2021-05-15  3    c

Related Articles

Furthermore, you may have a look at the other articles on this website:

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top