R Weekly Bulletin Vol – VII

[This article was first published on R programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)

“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


# 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.


now_time = Sys.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
[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
[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())
[1] “2017-05-06”

x = now_time + 1
[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.


# 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.

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.


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)

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)

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


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))

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)


Example 2:

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


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.

The post R Weekly Bulletin Vol – VII appeared first on .

To leave a comment for the author, please follow the link and comment on their blog: R programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)