join year month day into date in R

How to join year, month, and day into a date in R

Here is how to join year, month, and day into a date in R if they are stored separately. There are at least two good ways how to combine them in date. In some situations, there might be only a year and month and creating a date for the first day of the month is necessary.

 

Here is my data set. As you can see, there is a column with year, month, and day numbers. I want to merge those three columns into one date column.

df <- data.frame(
  yearnum = rep.int(2022, 6),
  monthnum = 1:6,
  daynum = c(10, 4, 12, 30, 9, 10)
)

df

#  yearnum monthnum daynum
#1    2022        1     10
#2    2022        2      4
#3    2022        3     12
#4    2022        4     30
#5    2022        5      9
#6    2022        6     10

 

Join year, month, and day with the help of the lubridate package

The easiest way to combine year, month, and day numbers into a date in R, in my opinion, is with the function make_date from the lubridate package.

require(lubridate)

make_date(year = df$yearnum, month = df$monthnum, day = df$daynum)

#[1] "2022-01-10" "2022-02-04" "2022-03-12" "2022-04-30" "2022-05-09" "2022-06-10"

In this scenario, if you want to gate the first date of each month, replace the day number column with 1.

make_date(year = df$yearnum, month = df$monthnum, day = 1)

#[1] "2022-01-01" "2022-02-01" "2022-03-01" "2022-04-01" "2022-05-01" "2022-06-01"

You can use that in a dplyr pipe to merge the year, month, and day column into the date column.

require(dplyr)


df %>% mutate('date' = make_date(year = yearnum, month = monthnum, day = daynum))

#  yearnum monthnum daynum       date
#1    2022        1     10 2022-01-10
#2    2022        2      4 2022-02-04
#3    2022        3     12 2022-03-12
#4    2022        4     30 2022-04-30
#5    2022        5      9 2022-05-09
#6    2022        6     10 2022-06-10

It is worth mentioning make_datetime if you want to create a DateTime from separate components.

 

Join year, month, and day into a date with base R

If you want to use R base functionality to combine year, month, and day components into a date, then try the function ISODate.

ISOdate(year = df$yearnum,
        month = df$monthnum,
        day = df$daynum)

#[1] "2022-01-10 12:00:00 GMT" "2022-02-04 12:00:00 GMT" "2022-03-12 12:00:00 GMT"
#[4] "2022-04-30 12:00:00 GMT" "2022-05-09 12:00:00 GMT" "2022-06-10 12:00:00 GMT"

This function creates an object of classes POSIXct, POSIXlt.

If you like to convert that to date class, then here is how to do that.

as.Date(ISOdate(year = df$yearnum,
                month = df$monthnum,
                day = df$daynum))

#[1] "2022-01-10" "2022-02-04" "2022-03-12" "2022-04-30" "2022-05-09" "2022-06-10"

 

Concatenate year, month, and day in R

Another option is to concatenate date components together and then convert the string to date. Click here to learn more about concatenating in R.

as.Date(paste(df$yearnum, df$monthnum, df$daynum, sep = "-"))

#[1] "2022-01-10" "2022-02-04" "2022-03-12" "2022-04-30" "2022-05-09" "2022-06-10"

If you prefer to merge columns with date components into a new data frame column, then here is how to do that.

df$date <- as.Date(paste(df$yearnum, df$monthnum, df$daynum, sep = "-"))

 

Take a look at how to combine only year and month or do the same with year and quarter.





Posted

in

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *