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
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" )
data_frame <- data.frame (datetime = as.POSIXct (
vec, format = "%Y-%m-%d %H:%M:%S" ))
print ( "Original DataFrame" )
print (data_frame)
data_frame$time <- format ( as.POSIXct (
data_frame$datetime),format = "%H:%M:%S" )
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" )
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" )
data_frame <- data.frame (datetime = ymd_hms (vec))
print ( "Original DataFrame" )
print (data_frame)
data_frame$time <- format ( as.POSIXct (
data_frame$datetime),format = "%H:%M:%S" )
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
4 min read
Shift a column of lists in data.table by group in R
In this article, we will discuss how to shift a column of lists in data.table by a group in R Programming Language. The data table subsetting can be performed and the new column can be created and its values are assigned using the shift method in R. The type can be specified as either "lead" or "lag
2 min read
Create timestamp variable in bash script
Handling timestamps is a common need in Bash scripting for a number of tasks, including file naming, data processing, and logging. Tracking events or organizing data can be made easier with the help of timestamps, which serve as a precise point-in-time representation. Creating timestamps in Bash scr
4 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
How to Get Current Date and Time in SQL?
Managing date and time efficiently is crucial for any database system. SQL provides built-in functions to retrieve the current date and time, which are especially useful for applications involving logging, reporting, and auditing. In this article, we will explain the three main SQL functions to fetc
4 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
Load .CSV Data into MySQL and Combine Date and Time
Prerequisite - How To Import Timestamp From a CSV File in MySQL? A CSV (comma-separated values) file is a text file with commas separating information. They are most commonly found in spreadsheets and databases and are now primarily used for datasets in data science and machine learning. They assist
4 min read
Split single column into multiple columns in PySpark DataFrame
pyspark.sql.functions provide a function split() which is used to split DataFrame string Column into multiple columns. Syntax: pyspark.sql.functions.split(str, pattern, limit=- 1) Parameters: str: str is a Column or str to split.pattern: It is a str parameter, a string that represents a regular expr
4 min read
Comparing Timestamp Dates With Date-Only Parameter in SQL
Working with date and time in databases is a common task, especially when dealing with timestamps. Comparing timestamp values with date-only parameters in SQL is important for filtering data, performing calculations, and ensuring data accuracy. In this article, we will explain the process of compari
4 min read
Split comma-separated strings in a column into separate rows
Splitting comma-separated strings in a column into separate rows is a common task in data manipulation and analysis in R Programming Language. This transformation is useful when dealing with data where multiple values are concatenated within a single cell, and you want to separate them into distinct
4 min read