Convert dataframe column to datetime in R
Last Updated :
23 Jul, 2025
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 frame
data_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 object
data_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 frame
data_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 object
data_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 frame
data_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 object
data_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"
Similar Reads
Convert DataFrame Column to Numeric in R In this article, we are going to see how to convert DataFrame Column to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type conversion,
9 min read
Convert Row Names into Column of DataFrame in R In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which
3 min read
Convert DataFrame with Date Column to Time Series Object in R In this article, we will discuss how to convert dataframe with date column to time series object in the R programming language. Time series object are a series of data points in which each data point is associated with a timestamp. For example, is a price of a stock in the stock market at different
2 min read
How to convert dataframe columns from factors to characters in R? In this article, we will discuss how to convert dataframe columns from factors to characters in R Programming Language. A dataframe can have different types of columns stacked together to form a tubular structure. Easy modification of the columns' data as well as conversion between data types can be
5 min read
How to convert DataFrame column from Character to Numeric in R ? In this article, we will discuss how to convert DataFrame column from Character to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type c
5 min read
How to Sort a DataFrame by Date in R? In this article, we will discuss how to sort a dataframe in R Programming Language. We can create a dataframe in R by using data.frame() function. In a dataframe we can create a date column using as.Date() function in the '%m/%d/%Y' format. Example: Let's create a dataframe with 2 columns including
2 min read