3MW (Extract days, months, etc. from your dates)

Guten Tag!

Many greetings from Ulm, Germany.

We are back this week with another installment on how to work with time data. But before we go into that, let me give you a short summary of the poll I ran last week regarding my ggplot2 course.

One common theme in the comments were that I should focus on the advanced stuff and not the basics. There are already lots of free, great resources for the basics, so I think that’s a great idea and I’ll do that. And while I work on that, you may enjoy my latest ggplot tutorial in which I show you how to create meaningful line charts like this:

Last week, we learned to parse time data and now we can work with that data. One thing I always catch myself doing is extracting the day, month, year and workday from the dates I’m working with. For example, this helps me to create calendar plot like this:

Let’s have a look at the tedious way so that we know what to avoid. Also, let me remind you that all of this week’s code can be found on GitHub.

Don’t do the tedious way

I’ve often taken the tedious road and computed the days, months and so on by manually using the corresponding functions from {lubridate}. This looked something like this:

Not very pretty, I know. So if you catch yourself applying all of these functions one by one, then stop. Here’s a better way.

Compute all time units all at once

The {timetk} package is a neat package that is especially suited to work with time data. After all, it is designed to help you with time series analysis.

Among the many useful functions from this package, you will find one function that takes care of all the previous function calls all at once. One call, that’s all!

Wooah that’s a lot of information right there. Maybe that’s a bit of an overload but it’s nice for flexing: All of that can be computed in one go. Cool, isn’t it?

Compute only your desired time units

Based on the original {timetk} function, here’s a little helper function that let’s you actually select which time units you want.

But what about non-tibble data?

As you’ve probably noticed, the previous functions worked only with tibbles. But what if you want to use a vector as input and extract time units from this?

Well, there’s another function tk_get_timeseries_signature() and it works just like you’d expect: You feed it with a vector of dates and it computes all the time units for these dates.

Of course, you could wrap this into another function that only computes the time units that you actually need. And like all math books I’ve read, we will leave this as an exercise for the interested reader.

That’s it for today. Hope you’ve enjoyed this week’s newsletter. If you want to reach out to me, just reply to this mail or find me on Twitter.

See next week,
Albert 👋

If you like my content, you may also enjoy these:

Reply

or to participate.