How can I add days to a date in R? Can you provide some examples?

How can I add days to a date in R? Can you provide some examples?

Adding days to a date in R can be done through the use of the “days()” function. This function allows you to specify the number of days to be added to a given date. For example, if you want to add 5 days to the date February 10th, 2020, you would use the code “days(5) + as.Date(“2020-02-10″)”. This would return the date February 15th, 2020. Similarly, you can subtract days by using a negative number in the “days()” function. This feature is useful in various data analysis tasks where manipulating dates is necessary.

Add Days to Date in R (With Examples)


You can use one of the following methods to add a certain number of days to a date in R:

Method 1: Use Base R

#create new column that adds 5 days to date column
df$date_plus5 <- as.Date(df$date) + 5

Method 2: Use lubridate Package

library(lubridate)

#create new column that adds 5 days to date column
df$date_plus5 <- ymd(df$date) + days(5)

The following examples show how to use each method with the following data frame:

#create data frame
df <- data.frame(date=c('2022-01-03', '2022-02-15', '2022-05-09',
                        '2022-08-10', '2022-10-14', '2022-12-30'),
                 sales=c(130, 98, 120, 88, 94, 100))

#view data frame
df

        date sales
1 2022-01-03   130
2 2022-02-15    98
3 2022-05-09   120
4 2022-08-10    88
5 2022-10-14    94
6 2022-12-30   100

Note: To subtract days from a date, simply change the addition sign to a subtraction sign in either of the formulas above.

Example 1: Add Days to Date Using Base R

The following code shows how to create a new column called date_plus5 that adds five days to each of the dates in the date column:

#create new column that adds 5 days to date column
df$date_plus5 <- as.Date(df$date) + 5#view updated data frame
df

        date sales date_plus5
1 2022-01-03   130 2022-01-08
2 2022-02-15    98 2022-02-20
3 2022-05-09   120 2022-05-14
4 2022-08-10    88 2022-08-15
5 2022-10-14    94 2022-10-19
6 2022-12-30   100 2023-01-04

Notice that the values in the new date_plus5 column are equal to the values in the date column with five days added to them.

We can also use the class() function to confirm that the new column is in a date format:

#display class of date_plus5 column
class(df$date_plus5)

[1] "Date"

Example 2: Add Days to Date Using lubridate Package

The following code shows how to use the ymd() and days() functions from the lubridate package to create a new column called date_plus5 that adds five days to each of the dates in the date column:

library(lubridate)

#create new column that adds 5 days to date column
df$date_plus5 <- ymd(df$date) + days(5)

#view updated data frame
df

        date sales date_plus5
1 2022-01-03   130 2022-01-08
2 2022-02-15    98 2022-02-20
3 2022-05-09   120 2022-05-14
4 2022-08-10    88 2022-08-15
5 2022-10-14    94 2022-10-19
6 2022-12-30   100 2023-01-04

Note: The ymd() function tells the lubridate package that the values in the date column are currently in a year-month-date format.

Refer to the lubridate for more date formatting options.

Cite this article

stats writer (2024). How can I add days to a date in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-days-to-a-date-in-r-can-you-provide-some-examples/

stats writer. "How can I add days to a date in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-days-to-a-date-in-r-can-you-provide-some-examples/.

stats writer. "How can I add days to a date in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-days-to-a-date-in-r-can-you-provide-some-examples/.

stats writer (2024) 'How can I add days to a date in R? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-days-to-a-date-in-r-can-you-provide-some-examples/.

[1] stats writer, "How can I add days to a date in R? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I add days to a date in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top