<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-weekly-bulletin-vol-vii/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-vii/"> R programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 1200px) 100vw, 1200px" alt="" class="wp-image-8499" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/05/R-Weekly-Vol-II-1.jpg?w=450&ssl=1" data-recalc-dims="1"></p>
<p>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. </p>
<div></div>
<p>	<a data-link="https://twitter.com/share?text=Learn+some+interesting+R+programming+functions.+%40QuantInsti&amp;url=https://www.quantinsti.com/blog/r-weekly-bulletin-vol-vii/" target="_blank" rel="nofollow" href="https://twitter.com/share?text=Learn+some+interesting+R+programming+functions.+%40QuantInsti&url=https://www.quantinsti.com/blog/r-weekly-bulletin-vol-vii/"><span><span>We will also cover functions like the identical function, all.equal function, any and all functions. </span><span>Click To Tweet<i></i></span></span></a>Hope you like this R weekly bulletin. Enjoy reading!</p>
<h3>Shortcut Keys</h3>
<p>1. Run current line/selection – Ctrl+Enter<br>
2. Move cursor to beginning of line – Home key<br>
3. Move cursor to end of line – End key</p>
<h3>Problem Solving Ideas</h3>
<h4><strong>Creating a sequence of dates using the </strong>seq<strong>.Date function</strong></h4>
<p>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:</p>
<p>seq.Date(from, to, by)</p>
<p>Where,<br>
“from” is the starting date.<br>
“to” is the end date. This can be an optional argument.<br>
“by” is the increment of the sequence.<br>
“length.out” takes an integer value; this is an optional argument, and indicates the desired length of<br>
the sequence</p>
<p><strong>Examples:</strong></p>
<pre># by month
seq(as.Date(&quot;2016/1/1&quot;), by = &quot;month&quot;, length.out = 12)</pre>
<p>[1] “2016-01-01” “2016-02-01” “2016-03-01” “2016-04-01” “2016-05-01”<br>
[6] “2016-06-01” “2016-07-01” “2016-08-01” “2016-09-01” “2016-10-01”<br>
[11] “2016-11-01” “2016-12-01”</p>
<pre># by quarters.
seq(as.Date(&quot;2000/1/1&quot;), as.Date(&quot;2003/1/1&quot;), by = &quot;quarter&quot;)</pre>
<p>[1] “2000-01-01” “2000-04-01” “2000-07-01” “2000-10-01” “2001-01-01”<br>
[6] “2001-04-01” “2001-07-01” “2001-10-01” “2002-01-01” “2002-04-01”<br>
[11] “2002-07-01” “2002-10-01” “2003-01-01”</p>
<pre># 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(&quot;2000/1/1&quot;), as.Date(&quot;2003/1/1&quot;), by = &quot;2 quarter&quot;)</pre>
<p>[1] “2000-01-01” “2000-07-01” “2001-01-01” “2001-07-01” “2002-01-01”<br>
[6] “2002-07-01” “2003-01-01”</p>
<h4>Adding a number to a date &amp; time variable</h4>
<p>There are three date &amp; 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.</p>
<p><strong>Example:</strong></p>
<pre>now_time = Sys.time()
print(now_time)</pre>
<p>[1] “2017-05-06 21:01:19 IST”</p>
<pre># Let us add a value of 1 to the now_time date variable.
x = now_time + 1
print(x)</pre>
<p>[1] “2017-05-06 21:01:20 IST”</p>
<p>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.</p>
<pre>now_time = Sys.time()
y = now_time + 86400
print(y)</pre>
<p>[1] “2017-05-07 21:01:20 IST”</p>
<p>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.</p>
<pre>now_time = as.Date(Sys.time())
print(now_time)</pre>
<p>[1] “2017-05-06”</p>
<pre>x = now_time + 1
print(x)</pre>
<p>[1] “2017-05-07”</p>
<h4>Converting American date format to standard format</h4>
<p>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.</p>
<p><strong>Example:</strong></p>
<pre># date in American format
dt = &quot;07/24/2016&quot;


# 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)</pre>
<p>Error in charToDate(x): character string is not in a standard unambiguous format</p>
<pre># Correct way of formatting the date
as.Date(dt, format = &quot;%m/%d/%Y&quot;)</pre>
<p>[1] “2016-07-24”</p>
<h3>Functions Demystified</h3>
<h4>identical function</h4>
<p>The identical function tests whether two objects in R are exactly equal. The objects to be compared<br>
are included as the arguments to the function, and the function returns a logical TRUE/FALSE as<br>
the output.</p>
<p><strong>Examples:</strong></p>
<pre>y1 = c(1:12)
y2 = c(1:12)
identical(y1, y2)</pre>
<p>[1] TRUE</p>
<pre>days = c(&quot;Mon&quot;, &quot;Tues&quot;, &quot;Wed&quot;, &quot;Thurs&quot;, &quot;Fri&quot;, &quot;Sat&quot;, &quot;Sun&quot;)
months = c(&quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;)
identical(days, months)</pre>
<p>[1] FALSE</p>
<h4>all.equal function</h4>
<p>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:</p>
<p><strong>all.equal(target, current)</strong></p>
<p>where,<br>
target is an R object.<br>
current is other R object, to be compared with target.</p>
<p><strong>Example:</strong></p>
<pre>all.equal(3, 2)</pre>
<p>[1] “Mean relative difference: 0.3333333”</p>
<pre># Using is.TRUE will return a logical value instead of the differences
# report.
isTRUE(all.equal(3, 2))</pre>
<p>[1] FALSE</p>
<h4>any and all functions</h4>
<p>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.</p>
<p><strong>Example 1:</strong></p>
<pre>sample = c(FALSE, FALSE, FALSE)
any(sample)</pre>
<p>[1] FALSE</p>
<pre>all(sample)</pre>
<p>[1] FALSE</p>
<p><strong>Example 2:</strong></p>
<pre>sample = c(TRUE, FALSE, FALSE)
any(sample)</pre>
<p>[1] TRUE</p>
<pre>all(sample)</pre>
<p>[1] FALSE</p>
<h3><strong>Next Step</strong></h3>
<p>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.</p>
<div><a target="_blank" rel="nofollow" href="https://www.quantinsti.com/wp-content/uploads/2017/05/QuantInsti%E2%84%A2_R_Bulletin-VII.pdf">Download the PDF Now!</a></div>
<p>The post <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-vii/">R Weekly Bulletin Vol – VII</a> appeared first on <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/"></a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-vii/"> R programming</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/0pl0-oT-5Yw"></a></div>