Strings can also contain information about dates.
In this exercise, we see how to transform the string to a POSIXct
object and how to extract the day, month, year, etc. from the object.
Consider the following toy dataframe, containing a date and time indication:
> df_date
description
1 10/9/2009 10:13:36
2 10/15/2009 16:07:50
3 10/24/2009 12:45:32
We use the as.POSIXct()
function to convert the string to a POSIXct
object. In the format
argument, we need to specify how the string is formatted.
For example:
%d
: Day of the month as decimal number (01–31).%m
: Month as decimal number (01–12).%Y
: Year with century.
You can find the full list of conversion specifications here1.First, we test our format argument on a single value from the dataframe.
Pay attention to how the conversion specifications and delimiters (backslash \
and colon :
) in the format argument map to the description.
> as.POSIXct("10/9/2009 10:13:36", format="%m/%d/%Y %H:%M:%S")
[1] "2009-10-09 10:13:36 CEST"
Now, we can apply this to the entire column:
> df_date$posix <- as.POSIXct(df_date$description, format="%m/%d/%Y %H:%M:%S")
> df_date
description posix
1 10/9/2009 10:13:36 2009-10-09 10:13:36
2 10/15/2009 16:07:50 2009-10-15 16:07:50
3 10/24/2009 12:45:32 2009-10-24 12:45:32
Finally, we can extract date elements (day, month, year, etc.) from the POSIXct
object. Imagine that the year would be an interesting predictor.
We extract this with the format()
function which takes the format
argument.
> df_date$year <- format(df_date$posix, format="%Y")
> df_date
description posix year
1 10/9/2009 10:13:36 2009-10-09 10:13:36 2009
2 10/15/2009 16:07:50 2009-10-15 16:07:50 2009
3 10/24/2009 12:45:32 2009-10-24 12:45:32 2009
Consider the following toy dataframe:
df_date <- data.frame(description=c("25-10-07", "12-11-11" , "29-01-17"))
"25-10-07"
(check the hyperlink with conversion specifications).
Store the resulting POSIXct
object in posix_try
.description
. Add a new column posix
to the df_date
dataframe containing the POSIXct
objects.month
.