Split Date-Time column into Date and Time variables in R
Last Updated :
09 Nov, 2021
R programming language provides a variety of ways for dealing with both date and date/time data. The builtin framework as.Date function is responsible for the handling of dates alone, the library chron in R handles both dates and times, without any support for time zones; whereas the POSIXct and POSIXlt classes provides the support for handling datetime objects as well as timezones. Easy conversion of date time objects can be performed to other date related objects.
Method 1. : Using POSIXct object
A date string can be first converted to POSIXct objects and then basic arithmetic can be performed on it easily. POSIXct objects ease the process of mathematical operations since they rely on seconds as the major unit of time management. The dates are converted to standard time zone, UTC. A string type date object can be converted to POSIXct object, using the as.POSIXct(date) method in R.
Syntax:
as.POSIXct ( date , format)
Parameter :
date - The string date object
format - The format specifier of the date
The date objects are stored as the number of days calculated starting January 1, 1970, where negative numbers are used to refer earlier dates. The Date objects support basic arithmetic directly, where in the integers are added or subtracted directly from the Dates. n number of days are added or subtracted directly and the standard date format is returned as an output. The Date object can also specify different formats to contain the dates. The as.Date() method takes as input a string date object and converts it to a Date object.
Syntax:
as.Date(character date object)
The format() method in R is used to format the specified date time object in the required format.
Syntax:
format (datetime , format = )
Example:
R
# declaring a datetime vector
vec <- c("2021-05-08 08:32:07","2021-07-18 00:21:07",
"2020-11-28 23:32:09","2021-05-11 18:32:07")
# creating datetime column in the dataframe
data_frame <- data.frame(datetime = as.POSIXct(
vec, format = "%Y-%m-%d %H:%M:%S"))
print ("Original DataFrame")
print (data_frame)
# extracting time
data_frame$time <- format(as.POSIXct(
data_frame$datetime),format = "%H:%M:%S")
# extracting date
data_frame$date <- as.Date (data_frame$datetime)
print ("Modified DataFrame")
print (data_frame)
Output
[1] "Original DataFrame"
datetime
1
2021-05-08 08:32:07
2
2021-07-18 00:21:07
3
2020-11-28 23:32:09
4
2021-05-11 18:32:07
[1] "Modified DataFrame"
datetime time date
1 2021-05-08 08:32:07 08:32:07 2021-05-08
2 2021-07-18 00:21:07 00:21:07 2021-07-17
3 2020-11-28 23:32:09 23:32:09 2020-11-28
4 2021-05-11 18:32:07 18:32:07 2021-05-11
Method 2 : Using lubridate package
Lubridate package in R programming language is used to work with date and time objects. It makes it easier to parse and manipulate the objects and needs to be installed and loaded into the working space.
The Sys.time() function in R is used to fetch the current date and time object according the IST zone. The hours() method in R is used to take an input an integer denoting the number of hours. The "lubridate" package objects allow direct arithmetic over its various components, therefore the number of hours can be directly subtracted from the lubridate time object. A result is also an object belonging to this class.
Sys.Date() function is used to return the system’s date.
Syntax: Sys.Date()
Parameters:
Does not accept any parameters
The ymd_hms() method in R is used to input a datetime object into the working space.
Example:
R
library("lubridate")
# declaring a datetime vector
vec <- c("2021-05-08 08:32:07","2021-07-18 00:21:07",
"2020-11-28 23:32:09","2021-05-11 18:32:07")
# creating datetime column in the dataframe
data_frame <- data.frame(datetime = ymd_hms(vec))
print ("Original DataFrame")
print (data_frame)
# extracting time
data_frame$time <- format(as.POSIXct(
data_frame$datetime),format = "%H:%M:%S")
# extracting date
data_frame$date <- as.Date (data_frame$datetime)
print ("Modified DataFrame")
print (data_frame)
Output
[1] "Original DataFrame"
datetime
1
2021-05-08 08:32:07
2
2021-07-18 00:21:07
3
2020-11-28 23:32:09
4
2021-05-11 18:32:07
[1] "Modified DataFrame"
datetime time date
1 2021-05-08 08:32:07 08:32:07 2021-05-08
2 2021-07-18 00:21:07 00:21:07 2021-07-17
3 2020-11-28 23:32:09 23:32:09 2020-11-28
4 2021-05-11 18:32:07 18:32:07 2021-05-11
Similar Reads
Split Text String in a data.table Column Using R In data manipulation tasks, especially when working with text data, you often need to split strings in a column and expand the results into multiple columns. The data.table package in R offers efficient methods to handle such operations, making it easy to perform complex data transformations. This a
3 min read
Handle date and time columns using R Managing date and time data is a crucial aspect of data analysis, as many datasets involve temporal information. R Programming Language is used for statistical computing and data analysis and provides several functions and packages to handle date and time columns effectively. Here we cover various t
11 min read
Filter multiple values on a string column in R using Dplyr In this article we will learn how to filter multiple values on a string column in R programming language using dplyr package. Method 1: Using filter() method filter() function is used to choose cases and filtering out the values based on the filtering conditions. Syntax: filter(df, condition) Parame
3 min read
How to separate date and time in R ? In this article, we are going to separate date and time in R Programming Language.  Date-time is in the format of date and time (YYYY/MM/DD HH:MM:SS- year/month/day Hours:Minute:Seconds). Extracting date from timestamp: We are going to extract date by using as.Date() function. Syntax:  as.Date(data
2 min read
Parsing Date and Time in R Programming - strptime() Function strptime() function in R Language is used to parse the given representation of date and time with the given template. Syntax: strptime(x, format, tz = "")Parameters: x: given representation of date and time y: given template in which parsing is done tz: a character string specifying the time zone t
1 min read
Parsing Date and Time in R Programming - strptime() Function strptime() function in R Language is used to parse the given representation of date and time with the given template. Syntax: strptime(x, format, tz = "")Parameters: x: given representation of date and time y: given template in which parsing is done tz: a character string specifying the time zone t
1 min read