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:

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



Questions

Consider the following toy dataframe:

df_date <- data.frame(description=c("25-10-07", "12-11-11" , "29-01-17"))
  1. Try to find the format that matches the first description "25-10-07" (check the hyperlink with conversion specifications). Store the resulting POSIXct object in posix_try.
  2. Now, use the format to handle the entire column description. Add a new column posix to the df_date dataframe containing the POSIXct objects.
  3. Extract the month from each object. Store this in the column month.