How to Create a Date in R

04.25.2021

Let's say you have a year, month, and day and you would like to create a date in R. In this article, we will learn how to create a date in R.

To create a date in R, we use the ISOdate method. The signature of this method is ISOdate(year, month, day).

We simply pass the year, month, and day then R will return us a POSIXct object, which is a more advance object.

year = 2020
month = 04
day = 22

ISOdate(year, month, day)
# [1] "2020-04-22 12:00:00 GMT"

Now, if we would like a simple date, we can convert this POSIXct object using the as.Date method.

year = 2020
month = 04
day = 22

adv.date = ISOdate(year, month, day)

as.Date(adv.date)

# [1] "2020-04-22"