How to convert a text column into dates (in R)
Task
When loading data, many software systems make intelligent guesses about the format and data type of each column, but sometimes that is not sufficient. If you have a column of text that should be interpreted as dates, how can we ask the software to convert it?
Solution
Let’s create a small example DataFrame to use here (using the method from how to create a data frame from scratch). Naturally, you would apply this solution to your own data instead.
1
2
3
4
5
df <- data.frame(
Date = c( '5/7/19', '5/10/19', '5/11/19' ),
Event = c( 'Work', 'Party', 'More work' )
)
df
1
2
3
4
Date Event
1 5/7/19 Work
2 5/10/19 Party
3 5/11/19 More work
We use the as.Date()
function to convert a text column into dates.
If the input dates are not in the standard format, we can use the format=
argument to change the format.
Note the difference between %y
and %Y
: The %y
code means a 2-digit year, but the %Y
code means a 4-digit year.
1
2
df$Date = as.Date( df$Date, format='%m/%d/%y' )
df
1
2
3
4
Date Event
1 2019-05-07 Work
2 2019-05-10 Party
3 2019-05-11 More work
It’s often easier to handle date conversions while reading the data file.
You can use the read_csv()
function in the readr
package,
which will automatically recognize dates in some common formats.
Additionaly, you can use the anytime()
function in the anytime
package
to automatically parse strings as dates regardless of the format.
1
2
3
4
# Use anytime() to attempt to parse various formats:
library(anytime)
examples <- c( "Nov 01 2022", "2022-11-01", "22-11-01" )
anytime( examples )
1
[1] "2022-11-01 UTC" "2022-11-01 UTC" NA
Note that it succeeded in two cases, but not the third.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Ni Shi (shi_ni@bentley.edu)