Dealing with date variables is usually tedious. When reading in a text data file into R, R will not automatically convert the date values into dates, rather, it will treat them as strings (make sure you specify stringsAsFactors = FALSE in your read.table() or read.csv() function. Otherwise, R will read in them as factors, which you don’t want). Sometimes, the date values are of the format “yyyymmdd”, like “20051023”, and R will treat them as numbers. It’s your job to convert these strings or numbers to dates. I’ve written a function that allows you to do that easily.
Here’re some examples of how to use it:
After you have converted the date variables from strings or numbers to date types, you often will want to extract from them the day, month, and year. Here’s how: