# How to 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
```

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

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 documentation page for more date formatting options.