How to Combine Date and Time in R


Often you may want to combine date and time columns in a data frame in R to create a new column that contains datetimes.

There are two common ways to do so:

Method 1: Combine Date and Time Using Base R

df$datetime <- as.POSIXct(paste(df$date, df$time), format="%Y-%m-%d %H:%M:%S")

This particular example uses the paste() and as.POSIXct() functions from base R to combine the values from the date column and the time column in an existing data frame and store the values in a new column named datetime.

Method 2: Combine Date and Time Using lubridate Package

library(lubridate)

df$datetime <- with(df, ymd(df$date) + hms(df$time))

This particular example uses the ymd() and hms() functions from the lubridate package in R to combine the values from the date column and the time column in an existing data frame and store the values in a new column named datetime.

The following examples show how to use each of these methods in practice with the following data frame in R:

#create data frame
df <- data.frame(date=c('2024-01-15', '2024-01-19', '2024-01-20', '2024-01-25'),
                 time=c('04:15:55', '12:34:18', '15:44:29', '18:34:55'),
                 sales=c(190, 234, 280, 318))


#view data frame
df

        date     time sales
1 2024-01-15 04:15:55   190
2 2024-01-19 12:34:18   234
3 2024-01-20 15:44:29   280
4 2024-01-25 18:34:55   318

This particular data frame shows the total number of sales made on various dates and on various times by an employee at some company.

Example 1: Combine Date and Time Using Base R

We can uses the following syntax to combine the values from the date and time columns in the data frame to create a new column named datetime:

#create new column named datetime
df$datetime <- as.POSIXct(paste(df$date, df$time), format="%Y-%m-%d %H:%M:%S")

#view updated data frame
df

        date     time sales            datetime
1 2024-01-15 04:15:55   190 2024-01-15 04:15:55
2 2024-01-19 12:34:18   234 2024-01-19 12:34:18
3 2024-01-20 15:44:29   280 2024-01-20 15:44:29
4 2024-01-25 18:34:55   318 2024-01-25 18:34:55

We can see that a new column named datetime has been created that combines the values from the date and time columns.

We can use the class() function to view the class of this new column:

#view class of datetime column
class(df$datetime)

[1] "POSIXct" "POSIXt" 

We can see that the datetime column has a class of POSIXct, which is a datetime class.

Example 2: Combine Date and Time Using lubridate Package in R

Another way to combine the values from the date and time columns in the data frame is to use functions from the lubridate package in R, which is a package that is designed to make handling dates and times easy.

Before using the package, we can first install it use the following syntax:

#install lubridate package
install.packages('lubridate')

Next, we can use the following syntax to combine the values from the date and time columns into a new column named datetime:

library(lubridate)

#create new column named datetime 
df$datetime <- with(df, ymd(df$date) + hms(df$time))

#view updated data frame
df

        date     time sales            datetime
1 2024-01-15 04:15:55   190 2024-01-15 04:15:55
2 2024-01-19 12:34:18   234 2024-01-19 12:34:18
3 2024-01-20 15:44:29   280 2024-01-20 15:44:29
4 2024-01-25 18:34:55   318 2024-01-25 18:34:55

We can see that a new column named datetime has been created that combines the values from the date and time columns.

We can use the class() function again to view the class of this new column:

#view class of datetime column
class(df$datetime)

[1] "POSIXct" "POSIXt" 

We can see that the datetime column has a class of POSIXct, which is a datetime class.

Notice that this produces the same result as the previous example using base R. Feel free to use whichever method you prefer.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Generate a Sequence of Dates in R
How to Convert Strings to Dates in R
How to Convert a String to Datetime in R
How to Calculate Number of Months Between Dates in R

Featured Posts

Leave a Reply

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