Tuesday 12 April 2016

Graphics, Magic and Vectorization in R Programming

Today we will take a look at Graphics, Magic Objects and Functions and Vectorization in the R Programming language. We will also deal with making mistakes deliberately in R, though in brief.

Graphics, Magic and Vectorization in R Programming
Graphics


If you want to have a picture in R you need a canvas in which it is present, which in R parlance translates to a graphics device. In case you are in the interactive process of creating graphs, you do not even need to bother about graphics devices as R will create a default device on your behalf. However when you want things so that you have graphs to share then you must necessarily arrive at a decision regarding a graphics device.

The primary function used in creating graphs is plot. It is not rare to find that a simple command like:

> plot(a)

will suffice. It is sometimes not the picture that you wish to see the most but often it does a decent job of arriving at a semi-sensible result.

It is not necessary for plots to be created in a single command as you always have the option of adding to plots open. To take an example:

 > abline(0, 1)

serves to add a single line of slope and intercepts 0 to the plot currently being discussed. It may or may not be visible, a trait which is dependent on the plot.

Magic Functions and Objects

Though magic is not the correct term to use in terms of official nomenclature, some objects and functions in R are said to be magic. Actually by the term magic objects we refer to objects with a class, and by magic functions we mean generic functions. When a generic function is used, it searches for its argument class. The actual action taking place is dependent on the class.

print and plot are two functions that are generic in their nature. Factors and data frames are individually printed in a way special to its own as print is generic while factors and data frames possess a class.
The advantage of print being a generic function is that you are able to see the object’s aspects that are important.

The disadvantage of print being generic lies in the fact that you may readily perceive the object being seen as the real one while in fact you are just seeing the portrait of the objects that is wanted to be seen by you by it.

Vectorization


R happens to be a vector language where it is highly improbable for an object to be just a single character string or number or logical value. It is far more probable for the object to have several values which may range from dozens to millions.

In an operation of vectorization an object is treated as a whole rather than dealing with individual values separately, like:

 > a + 3

This operation adds 3 to all of the values contained in a regardless of them being two or two thousand.

Deliberately Making Mistakes


This technique is used by many R professionals and seasoned users, deliberately make mistakes and experiment around. Two benefits associated with the same are:

•    You get to know how things function with reasonable efficiency.
•    You also get to know how to retain your calm just in case things go wrong.

One Last Tip

Lastly, it is highly recommended that you turn to R language training from good institutes like DexLab Analytics.

5 comments: