Hello, and welcome. In this video, we’ll show you how to work with calendar dates in the R programming language. R has a specialized object class for holding dates of a calendar. The output here may simply look like strings, but R actually represents a date as the number of days since January 1 of 1970. This particular representation for dates has become a standard across computer environments. And as a special note, the dates are based off of the Gregorian calendar. We’re going to practice with the date format, but first, let’s load up a database of Oscar-Winning actors. As you can see, the date of birth for each actor is represented in a non-standard format. By checking the structure of the data frame with the “str” function, we see that the “date of birth” column is of type “int”. As it turns out, this number is represented in the UNIX time format, another standard that counts the number of seconds since the beginning of January 1, 1970. So we’re going to have to reformat these values to work with R’s date representation. In order to do this, we’ll use the “as.POSIXct” function. POSIX is just another date format standard that R utilizes for its Date class structure. But besides passing in the column we want to convert, we also need to provide the function with a reference point, which will be January 1 of 1970. The function’s output will be a full timestamp, which will have this structure. Since we’re only dealing with calendar dates, we don’t need the hours, minutes, and seconds. So we’ll need to use one more function to get to the format we need. The “as.Date” function takes in a character string or a POSIX timestamp, and converts it into a Date Class object. You can see the final result in the output here. Now let’s consider another data frame that stores some information about Oscar-Winning Actresses. Like before, we can pass this data frame to the “str” function in order to see its structure. We’ve run into another issue, since the “date of birth” column holds factors, and not the Date class. So we’ll need to convert this column as well. Once again we’ll use “as.Date”, which works with both factors and character types. The additional parameter tells the function to expect the date format to be “Year Month Day”, in that order, separated by slashes. Doing this will successfully convert the column to the Date class. Let’s look at another example to understand the date formatting parameter. The first argument to the function is the string that we want to transform into a date object. The second argument once again tells R that the first argument has the structure “day month year” separated by slashes. As you can see here, R successfully parsed our string to form a Date object. R allows you to specify the format in detail using the codes here, but by default, R will assume the date is “year month day” separated by hyphens or slashes. Notice that since our second parameter has a lower case “y” for the year, R knew to look for a two-digit year representation. There are several operations you can use on Date objects. In this example, we simply subtract one date from another to get the number of days in between. Here, we use a comparator to see if one date comes after another. You can also subtract a number from a date. In this case, the output is a date that is 14 days back in time. There are also three functions that return the date and time based on the system. “System Date” returns the current date as a Date class object. The “date” function returns a character string describing the current date and time. And “System Time” returns the current time in a POSIX timestamp format. You can also apply some useful conversions on a Date Class Object. The “weekdays” function converts the date into a string representing the weekday that the date falls on. The “months” function works similarly. And there’s also a “quarters” function, which returns a string containing the quarter the date falls into. If you wish, you can convert your date to the Julian calendar. And as one last function, you can use the “seq” function to create a sequence of dates based on some reference point and length. This example starts at the current date, and moves forward in one month increments. By now, you should understand how to create dates in R, as well as how to apply various useful operations. Thank you for watching this video.