<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-iii/"></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-iii/"> 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: 1894px) 100vw, 1894px" alt="" class="wp-image-7925" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/Weekly-R-edition_volume_3.jpg?w=450&ssl=1" data-recalc-dims="1"></p>
<p>This week’s R bulletin will cover topics like how to read select columns, the difference between boolean operators, converting a factor to a numeric and changing memory available to R. We will also cover functions like data, format, tolower, toupper, and strsplit function. Hope you like this R weekly bulletin. Enjoy reading!</p>
<h3>Shortcut Keys</h3>
<p>1. To change the working directory – Ctrl+Shift+K<br>
2. To show history – Ctrl+4<br>
3. To display environment – Ctrl+8</p>
<h3>Problem Solving Ideas</h3>
<h4>Read a limited number of columns from a dataset</h4>
<p>To read a specific set of columns from a dataset one can use the “fread” function from the data.table package. The syntax for the function to be used for reading/dropping a limited number of columns is given as:</p>
<p>fread(input, select, stringsAsFactors=FALSE)<br>
fread(input, drop, stringsAsFactors=FALSE)</p>
<p>One needs to specify the columns names or column numbers with the select parameter as shown below.<br>
<strong>Example:</strong></p>
<pre>library(data.table)
data = fread(&quot;data.txt&quot;)
print(data)</pre>
<p>
<img sizes="(max-width: 724px) 100vw, 724px" alt="" class="wp-image-7929" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-Bulletin-3_Pic1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p> </p>
<pre># Reading select columns
data = fread(&quot;data.txt&quot;, select = c(&quot;DATE&quot;, &quot;OPEN&quot;, &quot;CLOSE&quot;))
print(data)</pre>
<p>
<img sizes="(max-width: 665px) 100vw, 665px" alt="" class="wp-image-7931" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-Bulletin-3_Pic2.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p> </p>
<p>Alternatively, you can use the drop parameter to indicate which columns should not be read:<br>
<strong>Example:</strong></p>
<pre>data = fread(&quot;data.txt&quot;, drop = c(2:3, 6))
print(data)</pre>
<p>
<img sizes="(max-width: 663px) 100vw, 663px" alt="" class="wp-image-7933" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-Bulletin-3_Pic3.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p> </p>
<h4>Difference between the boolean operators &amp; and &amp;&amp;</h4>
<p>In R we have the “&amp;” Boolean operator which is the equivalent to the AND operator in excel. Similarly, we have | in R which is the equivalent of OR in excel. One also finds Boolean operators &amp;&amp; and || in R. Although, these operators have the same meaning as &amp; and |, they behave in a slightly different manner.</p>
<p>Difference between &amp; and &amp;&amp;: The shorter form “&amp;” is vectorized, meaning it can be applied on a vector. See the example below.<br>
<strong>Example:</strong></p>
<p><img sizes="(max-width: 664px) 100vw, 664px" alt="" class="wp-image-7937" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-Bulletin-3_Pic4.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>The longer form &amp;&amp;, evaluates left to right examining only the first element of each vector. Thus in the example below, it will first evaluate whether -1 > = 0 (which is FALSE), and then check whether -1 &lt;= 0 (which is TRUE). After this, it will evaluate FALSE &amp; TRUE, which gives the final answer as FALSE.</p>
<p><strong>Example:</strong></p>
<p><img sizes="(max-width: 668px) 100vw, 668px" alt="" class="wp-image-7940" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-Bulletin-3_Pic5.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p> </p>
<p>Thus the &amp;&amp; operator is to be used when we have vectors of length one. For vectors of length greater than one, we should use the short form.</p>
<h4>How to convert a factor to a numeric without a loss of information</h4>
<p>Consider the following factor “x” created using the factor, sample, and the runif functions. Let us try to convert this factor to a numeric.</p>
<p><strong>Example:</strong></p>
<pre>x = factor(sample(runif(5), 6, replace = TRUE))
print(x)</pre>
<p>
[1] 0.660900804912671 0.735364762600511 0.479244165122509 0.397552277892828<br>
[5] 0.660900804912671 0.660900804912671<br>
4 Levels: 0.397552277892828 0.479244165122509 … 0.735364762600511</p>
<pre>as.numeric(x)</pre>
<p>
[1] 3 4 2 1 3 3</p>
<p>As can be seen from the output, upon conversion we do not get the original values. R does not have a function to execute such conversion error-free. To overcome this problem, one can use the following expression:</p>
<pre>as.numeric(levels(x))[x]</pre>
<p>
[1] 0.6609008 0.7353648 0.4792442 0.3975523 0.6609008 0.6609008</p>
<p>One can also use the as.numeric(as.character(x)) expression, however the as.numeric(levels(x))[x] is slightly more efficient in converting the factor to an integer/numeric.</p>
<h4>Replace a part of URL using R</h4>
<p>Suppose you want to scrap data from a site having different pages, and want to replace the url using R. We can do this by using the parse_url function and build_url function from the “httr” package. The following example illustrates the method to replace a part in url.</p>
<p><strong>Example:</strong></p>
<pre>library(httr)
# In the following url, the value 2 at the end signifies page 2 of the books
# catergory under the section &quot;bestsellers&quot;.
testURL = &quot;http://www.amazon.in/gp/bestsellers/books/1318158031/ref=zg_bs_nav_b_1_b#2&quot;

# Entering the url and parsing it using the parse_ulr function
parseURL = parse_url(testURL)
print(parseURL)</pre>
<p>
$scheme<br>
[1] “http”</p>
<p>$hostname<br>
[1] “www.amazon.in”</p>
<p>$port<br>
NULL</p>
<p>$path<br>
[1] “gp/bestsellers/books/1318158031/ref=zg_bs_nav_b_1_b”</p>
<p>$query<br>
NULL</p>
<p>$params<br>
NULL</p>
<p>$fragment<br>
[1] “2”</p>
<p>$username<br>
NULL</p>
<p>$password<br>
NULL</p>
<p>attr(,”class”)<br>
[1] “url”</p>
<pre># Assigning the next page number (i.e page no.3) and creating the new url
 parseURL$fragment = 3
 newURL &amp;lt;- build_url(parseURL)
 print(newURL)</pre>
<p>
[1] “http://www.amazon.in/gp/bestsellers/books/1318158031/ref=zg_bs_nav_b_1_b#3”</p>
<h4>Increasing (or decreasing) the memory available to R processes</h4>
<p>In R there is a memory.limit() function which gives you the amount of available memory in MB. At times, windows users may get the error that R has run out of memory. In such cases, you may set the amount of available memory.</p>
<p><strong>Example:</strong></p>
<p>memory.limit(size=4000)</p>
<p>The unit is MB. You may increase this value up to 2GB or the maximum amount of physical RAM you have installed. If you have R already installed and subsequently install more RAM, you may have to reinstall R in order to take advantage of the additional capacity.</p>
<p>On 32-bit Windows, R can only use up to 3Gb of RAM, regardless of how much you have installed. There is a 64-bit version of R for Windows available from REvolution Computing, which runs on 64-bit Windows, and can use the entire RAM available.</p>
<h3>Functions Demystified</h3>
<h4>data function</h4>
<p>R program includes a number packages which often come with different data sets. These data sets can be accessed using the data function. This function loads specified data sets or lists the available data sets. To check the available data sets from all the R packages installed in your application use the data() expression. This will display all the available data sets under each package.</p>
<p><strong>Example:</strong> data()<br>
To view data sets from a particular package, specify the package name as the argument to the data function.</p>
<p><strong>Example:</strong></p>
<pre>data(package = &quot;lubridate&quot;)</pre>
<p>
To load a particular dataset, use the name of the dataset as the argument to the data function.</p>
<p><strong>Example:</strong></p>
<pre># loading the USDCHF dataset from the timeSeries package
library(timeSeries)
data(USDCHF)
x = USDCHF
print(head(USDCHF))</pre>
<p>
<img sizes="(max-width: 668px) 100vw, 668px" alt="" class="wp-image-7946" src="https://i0.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-Bulletin-3_Pic6.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<pre># loading the sample_matrix dataset from the xts package
library(xts)
data(sample_matrix)
x = sample_matrix
print(head(sample_matrix))</pre>
<p>
 </p>
<h4> <img sizes="(max-width: 668px) 100vw, 668px" alt="" class="wp-image-7948" src="https://i1.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-Bulletin-3_Pic7.png?w=450&ssl=1" data-recalc-dims="1"></h4>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<h4><strong>format function</strong></h4>
<p>The format function is used to format the numbers so that they can appear clean and neat on the reports. The function takes a number of arguments to control the formatting of the result. The main arguments for the format function are given below.</p>
<p>format(x, trim, digits, decimal.mark, nsmall, big.mark, big.interval, small.mark,<br>
small.interval, justify)</p>
<p>Where,<br>
1) x: the number to be formatted. 2) trim: takes a logical value. If FALSE, it adds spaces to right-justify the result. If TRUE, it suppresses the leading spaces. 3) digits: how many significant digits of numeric values to display. 4) decimal mark: the format in which to display the decimal mark. 5) nsmall: the minimum number of digits after the decimal point. 6) big.mark: the mark between intervals before the decimal point. 7) small mark: the mark between intervals after the decimal point.</p>
<p><strong>Example:</strong></p>
<pre>format(34562.67435, digits=7, decimal.mark=&quot;.&quot;,big.mark=&quot; &quot;,
small.mark=&quot;,&quot;,small.interval=2)</pre>
<p>
[1] “34 562.67”</p>
<p>In the example above we chose to display 7 digits, and are using point(.) as a decimal mark. The small interval value is 2, which means we want 2 digits to be displayed after the decimal point. The big.mark format displays a space after the first two digits.</p>
<h4>tolower, toupper, and strsplit</h4>
<p>The tolower and toupper functions help convert strings to lowercase and uppercase respectively.<br>
<strong>Example 1:</strong></p>
<pre>toupper(&quot;I coded a strategy in R&quot;)</pre>
<p>
[1] “I CODED A STRATEGY IN R”</p>
<pre>tolower(&quot;I coded a strategy in R&quot;)</pre>
<p>
[1] “i coded a strategy in r”</p>
<p><strong>Example 2:</strong></p>
<pre>df = as.data.frame(read.csv(&quot;NIFTY.csv&quot;))
print(head(df, 4))</pre>
<p>
<img sizes="(max-width: 668px) 100vw, 668px" alt="" class="wp-image-7952" src="https://i2.wp.com/www.quantinsti.com/wp-content/uploads/2017/04/R-Weekly-Bulletin-3_Pic8.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p> </p>
<pre>colnames(df)</pre>
<p>
[1] “DATE” “TIME” “CLOSE” “HIGH” “LOW” “OPEN” “VOLUME”</p>
<pre>tolower(colnames(df))</pre>
<p>
[1] “date” “time” “close” “high” “low” “open” “volume”</p>
<p>The strsplit function is used to break/split strings at the specified split points. The function returns a list and not a character vector or a matrix. In the example below, we are splitting the expression on the spaces.</p>
<p><strong>Example:</strong></p>
<pre>strsplit(&quot;I coded a strategy in R&quot;, split = &quot; &quot;)</pre>
<p>
[[1]]<br>
[1] “I” “coded” “a” “strategy” “in” “R”</p>
<p><span></span></p>
<div><a target="_blank" rel="nofollow" href="https://www.quantinsti.com/wp-content/uploads/2017/04/QuantInsti_R_Bulletin-III.pdf"><i></i>Download the PDF Now!</a></div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>The post <a target="_blank" rel="nofollow" href="https://www.quantinsti.com/blog/r-weekly-bulletin-vol-iii/">R Weekly Bulletin Vol – III</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-iii/"> 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/X5iDd_hyfLo"></a></div>