R Weekly Bulletin Vol – VII

This week’s R bulletin will cover topics like how to create a sequence of dates, how to add a number to a date and time variable and converting a date in American format to a standard format.

We will also cover functions like the identical function, all.equal function, any and all functions. Click To TweetHope you like this R weekly bulletin. Enjoy reading!

Shortcut Keys

1. Run current line/selection – Ctrl+Enter
2. Move cursor to beginning of line – Home key
3. Move cursor to end of line – End key

Problem Solving Ideas

Creating a sequence of dates using the seq.Date function

The seq.Date function can be used for generating date sequences. The function works on dates with the class, “Date”. For dates with POSIX class, one can use the seq.POSIXt function for the same. The syntax for the seq.Date function is given as:

seq.Date(from, to, by)

Where,
“from” is the starting date.
“to” is the end date. This can be an optional argument.
“by” is the increment of the sequence.
“length.out” takes an integer value; this is an optional argument, and indicates the desired length of
the sequence

Examples:

# by month
seq(as.Date("2016/1/1"), by = "month", length.out = 12)
[1] “2016-01-01” “2016-02-01” “2016-03-01” “2016-04-01” “2016-05-01”
[6] “2016-06-01” “2016-07-01” “2016-08-01” “2016-09-01” “2016-10-01”
[11] “2016-11-01” “2016-12-01”
# by quarters.
seq(as.Date("2000/1/1"), as.Date("2003/1/1"), by = "quarter")
[1] “2000-01-01” “2000-04-01” “2000-07-01” “2000-10-01” “2001-01-01”
[6] “2001-04-01” “2001-07-01” “2001-10-01” “2002-01-01” “2002-04-01”
[11] “2002-07-01” “2002-10-01” “2003-01-01”
# If we specify a value in the 'by' argument, it will create a sequence of
# dates which are apart from that given value.
seq(as.Date("2000/1/1"), as.Date("2003/1/1"), by = "2 quarter")
[1] “2000-01-01” “2000-07-01” “2001-01-01” “2001-07-01” “2002-01-01”
[6] “2002-07-01” “2003-01-01”

Adding a number to a date & time variable

There are three date & time classes in R: POSIXct, POSIXlt, and Date. When we add a number to a POSIXct or POSIXlt class date, it takes the unit of the given number as seconds, and as a result it shifts the date by those many seconds.

Example:

now_time = Sys.time()
print(now_time)
[1] “2017-05-06 21:01:19 IST”
# Let us add a value of 1 to the now_time date variable.
x = now_time + 1
print(x)
[1] “2017-05-06 21:01:20 IST”

As can be seen from the output, the date shifts by 1 second. If we want to add 1 day to the “now_time” variable, we will have to add 86400 to the “now_time” variable as 1 day is equivalent to 86400 seconds. This will shift it forward by 1 day.

now_time = Sys.time()
y = now_time + 86400
print(y)
[1] “2017-05-07 21:01:20 IST”

However, if the date is stored as a Date class, then adding a value of 1 will shift it forward by 1 day. Thus, adding a number to a date variable in the form of Date class will shift it by that many days.

now_time = as.Date(Sys.time())
print(now_time)
[1] “2017-05-06”
x = now_time + 1
print(x)
[1] “2017-05-07”

Converting American date format to standard format

The American date format is of the type mm/dd/yyyy, whereas the ISO 8601 standard format is yyyy-mm-dd. To convert an American date format to the standard format we will use the as.Date function along with the format function. The example below illustrates the method.

Example:

# date in American format
dt = "07/24/2016"

# If we call the as.Date function on the date, it will throw up an error, as
# the default format assumed by the as.Date function is yyyy-mmm-dd.
as.Date(dt)

Error in charToDate(x): character string is not in a standard unambiguous format

# Correct way of formatting the date
as.Date(dt, format = "%m/%d/%Y")
[1] “2016-07-24”

Functions Demystified

identical function

The identical function tests whether two objects in R are exactly equal. The objects to be compared
are included as the arguments to the function, and the function returns a logical TRUE/FALSE as
the output.

Examples:

y1 = c(1:12)
y2 = c(1:12)
identical(y1, y2)
[1] TRUE
days = c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun")
months = c("Jan", "Feb", "Mar", "April", "May", "June", "July")
identical(days, months)
[1] FALSE

all.equal function

The all.equal function is used for checking equality of numbers. If the values to be compared are not the same, the all.equal function returns a report on the differences. The syntax for the function is given as:

all.equal(target, current)

where,
target is an R object.
current is other R object, to be compared with target.

Example:

all.equal(3, 2)
[1] “Mean relative difference: 0.3333333”
# Using is.TRUE will return a logical value instead of the differences
# report.
isTRUE(all.equal(3, 2))
[1] FALSE

any and all functions

The “any” function is used to check whether there is any true value in a given a set of logical vectors. The “all” function is another useful function which returns a TRUE if the all the values in the input logical vector are true.

Example 1:

sample = c(FALSE, FALSE, FALSE)
any(sample)
[1] FALSE
all(sample)
[1] FALSE

Example 2:

sample = c(TRUE, FALSE, FALSE)
any(sample)
[1] TRUE
all(sample)
[1] FALSE

Next Step

We hope you liked this bulletin. In the next weekly bulletin, we will list more interesting ways and methods plus R functions for our readers.