Adding useful labels

In the previous exercise you found the month of releases:

head(month(release_time))

and received numeric months in return. Sometimes it's nicer (especially for plotting or tables) to have named months. Both the month() and wday() (day of the week) functions have additional arguments label and abbr to achieve just that. Set label = TRUE to have the output labelled with month (or weekday) names, and abbr = FALSE for those names to be written in full rather than abbreviated.

For example, try running:

head(month(release_time, label = TRUE, abbr = FALSE))

Practice by examining the popular days of the week for R releases.

This exercise is part of the course

Working with Dates and Times in R

View Course

Exercise instructions

releases is now a data frame with a column called datetime with the release time.

  • First, see what wday() does without labeling, by calling it on the datetime column of releases and tabulating the result. Do you know if 1 is Sunday or Monday?
  • Repeat above, but now use labels by specifying the label argument. Better, right?
  • Now store the labelled weekdays in a new column called wday.
  • Create a barchart of releases by weekday, facetted by the type of release.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

library(ggplot2)

# Use wday() to tabulate release by day of the week
___(releases$datetime) %>% table()

# Add label = TRUE to make table more readable
___(releases$datetime, ___) %>% table()

# Create column wday to hold labelled week days
releases$wday <- ___

# Plot barchart of weekday by type of release
ggplot(releases, aes(___)) +
  geom_bar() +
  facet_wrap(~ type, ncol = 1, scale = "free_y")