94 LXF260 March 2020 http://www.linuxformat.com
CODING ACADEMY Dates & time
between two dates in days, whereas the second one
shows the same difference in minutes. Other valid units
are auto, secs, hours and weeks. Therefore, if you want
to calculate the difference between the current time and
Christmas 2020 in seconds you will need to execute
difftime(as.Date(“2020-12-25”), Sys.time(), units=”secs”).
Figure 2 (see page 92) illustrates some of the date-
related functions of R using the RStudio environment
for interacting with R.
Creating your first calendar
The following R code, which is saved as calendar.R, can
help you create your first calendar with the help of the
ggcal R package:
library(ggplot2)
library(ggcal)
date_range <- seq(as.Date(“2020-01-01”), as.Date(“2020-
01-31”), by=”1 day”)
fills <- rnorm(length(mydate))
print(ggcal(date_range, fills))
Note that in order to install ggcal, you will need to
execute the devtools::install_github(“jayjacobs/ggcal”)
command in R or RStudio, provided that you have
already installed the devtools R package.
Let us talk about what the previous R code does. The
first part of calendar.R is about loading the required R
packages, which in this case are just ggplot2 and ggcal.
Then, you can define the range of dates that you want to
get in your plot using the date_range variable. Next,
define the colours of days of the month – in this case
using a Normal Distribution with the help of the norm()
function. Print the calendar using the print(ggcal(date_
range, fills)) statement. The presented code offers
simplicity in exchange for a lack of customisation.
Figure 3 (see page 93) shows the graphical output of
the previous R commands. The default output of ggcal
is pretty naive – the good thing is that it enables you to
define the range of dates that you want to get on your
screen. Additionally, it colours each day of the month
with a pleasant colour.
The next version, which is saved as weekDays.R, will
create an output where you can easily differentiate
between weekdays and weekends:
library(ggplot2)
library(ggcal)
date_range <- seq(as.Date(“2020-03-01”), as.Date(“2020-
01-31”), by=”1 day”)
fills <- ifelse(format(date_range, “%w”) %in% c(0,6),
“weekend” ,“weekday”)
ggcal(date_range, fills) + scale_fill_manual(values=c(“w
eekday”=”darkgray”, “weekend”=”darkgreen”))
The magic happens at the definition of the fills variable:
> fills
[1] “weekday” “weekday” “weekday” “weekend”
“weekend” “weekday” “weekday” “weekday”
“weekday” “weekday” “weekend”
12] “weekend” “weekday” “weekday” “weekday”
“weekday” “weekday” “weekend” “weekend”
“weekday” “weekday” “weekday”
From this, each date of the month is either
characterised as weekday or weekend using the R %in%
operator. Before that, each day of the week is given a
number from 0 to 6 using format(date_range, “%w”).
Weekends are the days of the week with a number of 0
or 6 – this is what %in% catches and characterises as
[1] “2019-12-17 18:58:58 UTC”
> Sys.Date()
[1] “2019-12-17”
> format(Sys.Date(), “%Y”)
[1] “2019”
> format(Sys.Date(), “%B”)
[1] “December”
The first command prints the current date and time,
whereas the second command prints the current date
only. The third and fourth commands print the current
year using four digits and the full name of the current
month, respectively. Note that Sys.Date() returns an
object of class Date whereas Sys.time() returns an
object of class POSIXct.
Another useful capability is being able to extract the
month and the year out of a date that is stored in the
Epoch format – displaying dates and times in the UNIX
Epoch format has no practical meaning for the people
that are going to see a calendar or another kind of
output. The trick here is to convert the Epoch time into
a valid R object. The same can happen with a date and
time pair that is given as a string (such as 28 October
2019 18:30). However, in the latter case you will have to
define the format of the input string for R to convert it
into a valid object using format components. Both cases
are illustrated in the next interaction with the R shell:
> ep <- 1576610461
> as.POSIXct(ep, origin=”1970-01-01”)
[1] “2019-12-17 19:21:01 UTC”
> dateTime = “17/12/2019 21:15”
> myDate <- as.POSIXct(dateTime, format=”%d/%m/%Y
%H:%M”)
> myDate
[1] “2019-12-17 21:15:00 UTC”
The codes in the format string specification
(%d/%m/%Y %H:%M) should represent the format of
the string that is going to be converted into a POSIXct
object. If these are not correct, you will most likely get
an error message or, if you are very unlucky, an
incorrect value for your POSIXct object.
The following example shows how you can use the
difftime() function:
> d1 <- as.Date(“2018-12-22”)
> d2 <- as.Date(“2019-12-18”)
> difftime(d2, d1)
Time difference of 361 days
> difftime(d2, d1, units=”mins”)
Time difference of 519840 mins
The first difftime() function call shows the difference
Figure 4: This is a
much prettier and
more professional
calendar that was
created using the
capabilities of
ggplot2 and the
code of dates.R.
You can learn
more about
heat maps
at https://
en.wikipedia.
org/wiki/
Heat_map.
Additionally,
you can find
another way
of creating
calendars in
R by visiting
http://bit.ly/
lxf260calendar.
94 LXF260March 2020 9992March 0expl2i0x
CODING ACADEMY Dates & time
between two dates in days, whereas the second one
shows the same difference in minutes. Other valid units
are auto, secs, hours and weeks. Therefore, if you want
to calculate the difference between the current time and
Christmas 2020 in seconds you will need to execute
difftime(as.Date(“2020-12-25”), Sys.time(), units=”secs”).
Figure 2 (see page 92) illustrates some of the date-
related functions of R using the RStudio environment
for interacting with R.
Creating your first calendar
The following R code, which is saved as calendar.R, can
help you create your first calendar with the help of the
ggcal R package:
library(ggplot2)
library(ggcal)
date_range <- seq(as.Date(“2020-01-01”), as.Date(“2020-
01-31”), by=”1 day”)
fills <- rnorm(length(mydate))
print(ggcal(date_range, fills))
Note that in order to install ggcal, you will need to
execute the devtools::install_github(“jayjacobs/ggcal”)
command in R or RStudio, provided that you have
already installed the devtools R package.
Let us talk about what the previous R code does. The
first part of calendar.R is about loading the required R
packages, which in this case are just ggplot2 and ggcal.
Then, you can define the range of dates that you want to
get in your plot using the date_range variable. Next,
define the colours of days of the month – in this case
using a Normal Distribution with the help of the norm()
function. Print the calendar using the print(ggcal(date_
range, fills)) statement. The presented code offers
simplicity in exchange for a lack of customisation.
Figure 3 (see page 93) shows the graphical output of
the previous R commands. The default output of ggcal
is pretty naive – the good thing is that it enables you to
define the range of dates that you want to get on your
screen. Additionally, it colours each day of the month
with a pleasant colour.
The next version, which is saved as weekDays.R, will
create an output where you can easily differentiate
between weekdays and weekends:
library(ggplot2)
library(ggcal)
date_range <- seq(as.Date(“2020-03-01”), as.Date(“2020-
01-31”), by=”1 day”)
fills <- ifelse(format(date_range, “%w”) %in% c(0,6),
“weekend” ,“weekday”)
ggcal(date_range, fills) + scale_fill_manual(values=c(“w
eekday”=”darkgray”, “weekend”=”darkgreen”))
The magic happens at the definition of the fills variable:
> fills
[1] “weekday” “weekday” “weekday” “weekend”
“weekend” “weekday” “weekday” “weekday”
“weekday” “weekday” “weekend”
12] “weekend” “weekday” “weekday” “weekday”
“weekday” “weekday” “weekend” “weekend”
“weekday” “weekday” “weekday”
From this, each date of the month is either
characterised as weekday or weekend using the R %in%
operator. Before that, each day of the week is given a
number from 0 to 6 using format(date_range, “%w”).
Weekends are the days of the week with a number of 0
or 6 – this is what %in% catches and characterises as
[1] “2019-12-17 18:58:58 UTC”
> Sys.Date()
[1] “2019-12-17”
> format(Sys.Date(), “%Y”)
[1] “2019”
> format(Sys.Date(), “%B”)
[1] “December”
The first command prints the current date and time,
whereas the second command prints the current date
only. The third and fourth commands print the current
year using four digits and the full name of the current
month, respectively. Note that Sys.Date() returns an
object of class Date whereas Sys.time() returns an
object of class POSIXct.
Another useful capability is being able to extract the
month and the year out of a date that is stored in the
Epoch format – displaying dates and times in the UNIX
Epoch format has no practical meaning for the people
that are going to see a calendar or another kind of
output. The trick here is to convert the Epoch time into
a valid R object. The same can happen with a date and
time pair that is given as a string (such as 28 October
2019 18:30). However, in the latter case you will have to
define the format of the input string for R to convert it
into a valid object using format components. Both cases
are illustrated in the next interaction with the R shell:
> ep <- 1576610461
> as.POSIXct(ep, origin=”1970-01-01”)
[1] “2019-12-17 19:21:01 UTC”
> dateTime = “17/12/2019 21:15”
> myDate <- as.POSIXct(dateTime, format=”%d/%m/%Y
%H:%M”)
> myDate
[1] “2019-12-17 21:15:00 UTC”
The codes in the format string specification
(%d/%m/%Y %H:%M) should represent the format of
the string that is going to be converted into a POSIXct
object. If these are not correct, you will most likely get
an error message or, if you are very unlucky, an
incorrect value for your POSIXct object.
The following example shows how you can use the
difftime() function:
> d1 <- as.Date(“2018-12-22”)
> d2 <- as.Date(“2019-12-18”)
> difftime(d2, d1)
Time difference of 361 days
> difftime(d2, d1, units=”mins”)
Time difference of 519840 mins
The first difftime() function call shows the difference
Figure 4: This is a
much prettier and
more professional
calendar that was
created using the
capabilities of
ggplot2 and the
code of dates.R.
Youcanlearn
moreabout
heatmaps
athttps://
en.wikipedia.
org/wiki/
Heat_map.
Additionally,
youcanfind
anotherway
ofcreating
calendarsin
R byvisiting
http://bit.ly/
lxf260calendar.