Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Insert Rows for Missing Dates in R DataFrame

  • Last Updated : 23 Sep, 2021

In this article, we are going to see how to insert rows for missing dates in R Programming language.

The padr package in R is used to make preparations of time series data using the pad() function. The package can be downloaded and installed into the working space using the following command :

install.packages(“padr”)

The pad method in R is used to perform the date padding. It is used to pad all the observations between the first and last values obtained from the data frame. It itself figures out what the datetime variable in the data frame is, thereby assessing its interval. It performs an insertion for every lacking time point lacking in the dataset within the interval. NA or missing values are inserted into the data frame for all the non-datetime variable rows in the data frame.

pad(data-frame)

Example 1:

R




library("padr")
  
# creating data frame
data_frame <- data.frame(col1 = as.Date(c("2021-08-02"
                                    "2021-08-04",
                                    "2021-08-09",
                                    "2021-08-10")),
                   col2 = letters[1:4])
print("Data Frame")
print(data_frame)
  
# modified data
data_frame_mod <- pad(data_frame)
print(data_frame_mod)

Output:



The following code snippet doesn’t add any rows in the data frame since all the corresponding dates are within the same time interval, that is a period of 3 days. This information is displayed on the console while using the pad() method.

Example 2:

R




library("padr")
  
# creating data frame
data_frame <- data.frame(col1 = as.Date(c("2021-10-29"
                                    "2021-11-01",
                                    "2021-11-04"
                                    )),
                   col2 = letters[1:3])
print("Data Frame")
print(data_frame)
  
# modified data
data_frame_mod <- pad(data_frame)
print(data_frame_mod)

Output:

Also, the pad() method can be customized to append the intervals using ‘hours’ or ‘mins’ and specifying the starting and ending interval values using the start_val and end_val arguments respectively. The missing values are then appended in the specified intervals.

pad( 'hour' , start_val = , end_val = )



My Personal Notes arrow_drop_up
Recommended Articles
Page :