Convert dataframe column to datetime in R - GeeksforGeeks

23 May, 2021
The string-type date objects do not offer easy parsing and manipulation of the components. The conversion of date objects to POSIXct or POSIXlt objects can help in the easy conversion of dates to the required formats with desirable time zones. In this article, we will discuss how to convert dataframe column to string in R Programming Language.
Method 1: Using as.POSIXct() method
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 the standard time zone, UTC. A string type date object can be converted to POSIXct object, using them as.POSIXct(date) method in R. 
1 hour = 1 * 60 * 60 seconds
1 min = 1 * 60 seconds
“ct” in POSIXct denotes calendar time, it stores the number of seconds since the origin. It takes as input the string date object and the format specifier. POSIXct stores date and time in seconds with the number of seconds beginning from 1 January 1970.
as.POSIXct( date, format)
Code:
R
# declaring a data framedata_frame = data.frame(col1 = letters[1:4],                        col2 = c(5:8) ,                        col3 = c("2021-05-05 01:04:34",                                 "2021-03-06 03:14:44",                                 "2021-03-11 07:22:48",                                 "2021-02-02 11:54:56"))  print ("Original dataframe")print (data_frame)sapply(data_frame, class)  # converting to datetime objectdata_frame[['col3']] <- as.POSIXct(data_frame[['col3']],                                   format = "%Y-%m-%d %H:%M:%S")  print ("Modified dataframe")print (data_frame)sapply(data_frame, class)
Output:
[1] "Original dataframe"
 col1 col2                col3
1    a    5 2021-05-05 01:04:34
2    b    6 2021-03-06 03:14:44
3    c    7 2021-03-11 07:22:48
4    d    8 2021-02-02 11:54:56
    col1      col2      col3
"factor" "integer"  "factor"
[1] "Modified dataframe"
 col1 col2                col3
1    a    5 2021-05-05 01:04:34
2    b    6 2021-03-06 03:14:44
3    c    7 2021-03-11 07:22:48
4    d    8 2021-02-02 11:54:56
$col1
[1] "factor"

$col2
[1] "integer"

$col3
[1] "POSIXct" "POSIXt"
Method 2 : Using strptime() method
strptime method in R is used to directly convert character vectors (of a variety of formats) to POSIXlt format. strptime is faster than the previous approach because strptime only handles character input. 
Syntax: strptime(date, format, tz = “”)
Parameters: 
date – The date in character format
format – The format specifier of the input date
tz – time zone (optional)
Code:
R
# declaring a data framedata_frame = data.frame(col1 = letters[1:4],                        col2 = c(5:8) ,                        col3 = c("2021-05-05 01:04:34",                                 "2021-03-06 03:14:44",                                 "2021-03-11 07:22:48",                                 "2021-02-02 11:54:56"))  print ("Original dataframe")print (data_frame)sapply(data_frame, class)  # converting to datetime objectdata_frame[['col3']] <- strptime(data_frame[['col3']],                                 format = "%Y-%m-%d %H:%M:%S")  print ("Modified dataframe")print (data_frame)sapply(data_frame, class)
Output:
[1] "Original dataframe"
 col1 col2                col3
1    a    5 2021-05-05 01:04:34
2    b    6 2021-03-06 03:14:44
3    c    7 2021-03-11 07:22:48
4    d    8 2021-02-02 11:54:56
    col1      col2      col3
"factor" "integer"  "factor"
[1] "Modified dataframe"
 col1 col2                col3
1    a    5 2021-05-05 01:04:34
2    b    6 2021-03-06 03:14:44
3    c    7 2021-03-11 07:22:48
4    d    8 2021-02-02 11:54:56
$col1
[1] "factor"

$col2
[1] "integer"

$col3
[1] "POSIXlt" "POSIXt"
The format specifiers indicate the way to parse the character date object. It converts the datetime object into YYYY-MM-DD HH:MM:SS object. 
R
# declaring a data framedata_frame = data.frame(col1 = letters[1:4],                         col2 = c(5:8) ,                         col3 = c("15/12/2021 01:04:34",                                 "06/10/2021 03:14:44",                                 "11/04/2021 07:22:48",                                 "28/01/1994 11:54:56"))print ("Original dataframe")print (data_frame)sapply(data_frame, class)  # converting to datetime objectdata_frame[['col3']] <- strptime(data_frame[['col3']],                                 format = "%d/%m/%Y %H:%M:%S")  print ("Modified dataframe")print (data_frame)sapply(data_frame, class)
Output:
[1] "Original dataframe"
 col1 col2                col3
1    a    5 15/12/2021 01:04:34
2    b    6 06/10/2021 03:14:44
3    c    7 11/04/2021 07:22:48
4    d    8 28/01/1994 11:54:56
    col1      col2      col3
"factor" "integer"  "factor"
[1] "Modified dataframe"
 col1 col2                col3
1    a    5 2021-12-15 01:04:34
2    b    6 2021-10-06 03:14:44
3    c    7 2021-04-11 07:22:48
4    d    8 1994-01-28 11:54:56
$col1
[1] "factor"

$col2
[1] "integer"

$col3
[1] "POSIXlt" "POSIXt"
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Replace specific values in column in R DataFrame ?
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
Remove rows with NA in one column of R DataFrame
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?