0:00
Welcome to the Data All YouTube channel
0:02
In today's video, we are going to look into how do we work with dates in our
0:06
So, dates is an important part of when we are working with the data science project
0:11
And in this video, this is what we are going to explain. So first, let's load the library
0:16
We are going to work with the TIDIV's library. And more specifically, we are going to work with the Liberty Date library
0:22
It gets loaded with the Tidiverse. If you want to, you can only load the Lubridate library
0:28
but I usually have the idea of loading the tidewers and that loads all the library that are attached with it
0:38
So we have the CSV file. Let me show you the CSV. Now this is the CSV file that we have and we contains different kinds of elements related to date and time
0:49
And these are the dates that we are going to import. So we want to show you that when you import a data in R, what would happen if there is a date variable
0:58
it. So what we are going to do is we are going to use the read underscore CSV function to
1:05
import this date's CSV file and let's store it into this data object and let me show you this
1:11
data. So this is how it looks like. Now it looks like date but in some cases now this is
1:18
considered a string by let me show you the structure of this this data object. Now if you look
1:24
at the structure you would see that it reads these data as character variable
1:30
So it doesn't recognize them as date. And if you want to perform certain functions
1:34
that are related to date, let's say, Arthematics, et cetera. We want to extract the month or the year
1:41
from this date. Then we cannot perform these functions on these character data
1:46
We would have to tell are that these are the dates column
1:50
rather than character column. Now one way of doing it while we are importing it
1:55
is to use this column types function and within that we would use this code chunk where we tell that date one column
2:03
Now this is the date one column and this is the date five column and date four column
2:08
So these are the date and we would have to specify their format. So their format is that we first have the day, then we have the month and then we have the year
2:18
This is how I have specified it. Similarly for the case of date four, we have dashes instead of instead of four
2:25
instead of the slashes. And when we import this, and let me show you the structure of this data too
2:36
you would see that now it reads date one and date two
2:41
as a date column rather than a character column. This is how you can see here that it is a date rather than a character column
2:51
Now this is one way of reading the dates in our, that is while you're importing a CSV file
2:57
But what if you have a string within your data and you want to create a date or time
3:03
from that specific string? So we have over here a string because we know it
3:07
because it is in inverted commas. And if we want to convert this string
3:12
this character into a daytime object, there is a symbol function within a lubricate
3:17
that specifies, that converts the string into a date. And that is DMY
3:24
It is dim by because our date is written as the day, day first, then we have month, and then we have the year
3:32
So if I press control enter, you would see it is converted into a date
3:37
If I can show you the class of this object, then you would see that its class is a date class
3:45
This is how we know that it is a date. Similarly this is the same date but written in a different format We are using the same function and we are converting it into a date object So we use this DMY which means that day month and year we have similar other functions
4:06
MDY and YMD depending on how your date is written. So let's work with this data frame that we already have
4:14
We have this date one column and if we look at its
4:19
It's its data type, then it is a character data. Now we use this DMY function on this column to convert it into a date type and at the same time I'm going to show you the class of that, that object, that column and it says it's a date
4:41
So we show you that how do you use the DMY function. Similarly, we have MDY because if our date is written in the format that we have month first and then we have the day and then the year, then we would use the MDY
4:57
Or in other case, if we have year month and date, day, then we would use YMD
5:04
We can also work with the date time object. For example, we have this daytime object
5:11
We would use DMY because we have day month and day. year and HMS because we have our minutes and seconds and that would convert it into a
5:23
date time object similarly if we have month day and year then we would use MDI
5:31
rather than DMY let me show you the class of this this object it says it's a
5:39
POSIX city that's why we know that it is a date time object so we have three kinds of
5:44
classes when we are working with date and time we have date that we showed you were here
5:50
we have date time this is how it looks like and then we have a difference that we are going to
5:56
look into at the end of this tutorial now let's move forward and let's extract
6:03
different components from a date so let's say we have this date we have this data where
6:10
we have different component and we want to create date time from these components. So we have the day, month, and years, but we want to create a
6:22
date object. So what we do is we use the make date function where we specify the year, month
6:30
and day in the same order. These are the name of the columns that we are going to specify
6:35
So we take the data frame and then we mutate date of birth which should be equal to make date
6:44
from these three columns, the year, the month, and the day column
6:50
Now let's press control enter and if I can show you this column had been created and it is a date column
6:58
You can look at whether it is a date column or not using this class function
7:02
Similarly, we can make a date time object in a similar fashion because we have hours and
7:09
minutes and seconds in our data. So we can take and make a date time object
7:17
And this is how this date time object would look like. Now we combined these individual components in a date or daytime object
7:27
What if we have a date and we want to extract some date or time components from it
7:33
So let's look at this daytime object. We have this daytime object and let's create a date
7:41
object that would contain this daytime. And if we want to extract ear from this daytime we would use just the ear function and specify the object or the column that we want to extract the ear from Similarly we can extract month day
8:00
or minutes, seconds, the day of the month, the week of the week, the day of the year and the week
8:09
of the year. There is one more thing that we can do is for example when we extract the month
8:15
it shows us the number of that month. So we know that March is the third month of the year
8:24
Whatever if we wanted the label rather than the number of that month
8:31
then we'd use the label parameter and it gives us that it is March
8:35
And it gives us in the abbreviated form. And if you do not want it in the abbreviated form
8:41
then we would use the false, the abbreviated pattern. and set it to fall. By default, it is set to two and that would give us the full form of that month
8:54
Okay, so let's move forward and look into how do we round the dates. So let's take this date and we had created this date of birth object and if you want to round it
9:07
to the lower digit to the floor using the month's parameter like for example, let me show you should
9:15
show you this date. So we want, so this is 13th of January 1996. We want to round it off
9:24
We want to set it to a lower date based on the month. So we want to convert it into first of
9:31
January 1996. Similarly, all these days would be converted to the first of January 1996. This is
9:38
what the flow date would do. Similarly, the ceiling date would convert into the upper digit
9:43
but we again have to specify which kind of upper digit. We want the month or we want the week
9:51
So in this case, I have specified the week so we can show you how it would work
9:55
Similarly, round date would simply round the date to the nearest object
9:59
So if I can show you these three columns, we had this DOB, which was 13th of January 1996
10:06
It had been rounded to the 1st of January 1996. Similarly, the ceiling would take it to the higher digit
10:15
In this case, we took it to the higher day of the last day of that specific week rather than that specific month
10:23
So these are all from the same week. Similarly, these are all from the same week
10:28
You can also get the current date, the current time, or the current time
10:36
from using this today and now function, we can convert the date into daytime or date time into date
10:47
So let's say we have this today, this is a date. This is a date class
10:52
And if you want to convert into a date time, then we'll use as date time
10:56
We have this now, which is a date time object. And if you want to convert it into date only
11:04
then we would use as date. Now, lastly, let's look at the Arthmatics with the dates
11:09
How do we add, subtract, multiply the dates? And so for this, we would have to understand these three different concepts
11:18
One is called duration, which is measures in the seconds. The second is called period, which measures in the human readable format
11:26
It is date in the human readable format. Then we have intervals, which is, again, a range of date that have a starting and ending point
11:35
So let's understand how duration works. Let say we take the today date and deduct this specific date 30th of March 2024 from today date and create this diff object Now what this diff object would tell us that the difference between these two date is six days
11:56
And if you want to look at this class of this object, now remember I discussed over here that we have three kinds of objects
12:06
The last one is the diff time. Now this is the difference between
12:11
two dates or two times. So this is why it's a diff time object. So let us go back. So the duration
12:18
gives us answers. The duration gives us time in seconds, right? If you want to convert this
12:24
into duration, then we'll use the as duration. It would give us the number of seconds that we have
12:31
We know that it was six days, how many seconds it is, how many seconds of difference there is
12:37
between these two dates. We can get the seconds, so the minutes
12:46
sorry, how many seconds are there in 10 minutes, how many seconds there are in 10 hours
12:54
seconds in 10 days, seconds in 10 weeks, and seconds in 10 years
12:59
So these are the functions that would convert any time or diff time, it would rather work with the diff time
13:06
any diff time into seconds. Now, the seconds are fine, but the issue is that
13:13
let's say we have this time period. We have this 20th of January, 2024
13:18
and we want to convert it, we want to make it one year forward
13:23
We want to add one year into it. So let's do the years, this is the duration year, one into it
13:32
Let's add it. What we see is that we had to, 20th of January, 24, but somehow we cannot get the exact date
13:42
We should have got 20th of January 2025. The reason is that this D year or the duration does not take into account the leap year
13:53
2024 is a leap year. So it doesn't take into account the leap year
13:57
For that, we have periods. And within periods, we have these three functions, years, months, and days
14:04
Now, this gets confusing because. When you type ear, in lubricate we have ear and then we have ears
14:11
This ear is the function that extracts, that gets the ear component from a daytime object
14:19
whereas this ears get a parsed period. So this is a period object and this gets a ear from as we discussed previously
14:28
Similarly, we have D ears, which is the duration in seconds. Now, do not get confused with this
14:35
So this is the period years rather than extracting the year. So if we add this, we get exact time period, 20th of January 2025
14:48
Similarly, we can add one month. But do remember that I just discussed with you that there are different functions when you work with month or year
15:00
There is this month and then we have this month's function. Now this month's function is related to period whereas this month would extract one month
15:13
So this would extract one month and add it to it. So this would just be 21st of January 24
15:24
So if you want to add one month into the date, then you'd have to work with periods rather than duration or the month function
15:31
So that's why this is an incorrect function. So I hope this video was useful. Do subscribe to this channel. Do hit the bell icon and thanks for watching this video