Skip to content
Related Articles

Related Articles

Improve Article

Split Date-Time column into Date and Time variables in R

  • Last Updated : 06 Jun, 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 , fomat = )



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



My Personal Notes arrow_drop_up
Recommended Articles
Page :