Basics of R Programming

Why learn R?

R-programming

I don’t know if I have a solid reason to convince you, but let me share what got me started. I have no prior coding experience. Actually, I never had computer science in my subjects. I came to know that to learn data science, one must learn either R or Python as a starter. I chose the R. Here are some benefits I found after using R:

  1. The style of coding is quite easy.
  2. It’s open source. No need to pay any subscription charges.
  3. Availability of instant access to over 7800 packages customized for various computation tasks.
  4. The community support is overwhelming. There are numerous forums to help you out.
  5. Get high performance computing experience ( require packages)
  6. One of highly sought skill by analytics and data science companies.

There are many more benefits. And, if you aren’t convinced, you may like Complete Python Tutorial from Scratch.(Will be out in few weeks)

 

How to install R / R Studio ?

You could download and install the old version of R. But, I’d insist you to start with RStudio. It provides much better coding experience. For Windows users, R Studio is available for Windows Vista and above versions. Follow the steps below for installing R Studio:

  1.  Go to this link.
  2.  In ‘Installers for Supported Platforms’ section, choose and click the R Studio installer based on your operating system. The download should begin as soon as you click.
  3. Click Next..Next..Finish.
  4. Download Complete.
  5. To Start R Studio, click on its desktop icon or use ‘search windows’ to access the program. It looks like this:

rstudio script,console,environment,graphical outputLet’s quickly understand the interface of R Studio:

  1. R Console: This area shows the output of code you run. Also, you can directly write codes in console. Code entered directly in R console cannot be traced later. This is where R script comes to use.
  2. R Script: As the name suggest, here you get space to write codes. To run those codes, simply select the line(s) of code and press Ctrl + Enter. Alternatively, you can click on little ‘Run’ button location at top right corner of R Script.
  3. R environment: This space displays the set of external elements added. This includes data set, variables, vectors, functions etc. To check if data has been loaded properly in R, always look at this area.
  4. Graphical Output: This space display the graphs created during exploratory data analysis. Not just graphs, you could select packages, seek help with embedded R’s official documentation.

 

How to install R Packages ?

The sheer power of R lies in its incredible packages. In R, most data handling tasks can be performed in 2 ways: Using R packages and R base functions. In this tutorial, I’ll also introduce you with the most handy and powerful R packages. To install a package, simply type:

install.packages("package name")

As a first time user, a pop might appear to select your CRAN mirror (country server), choose accordingly and press OK.

Note: You can type this either in console directly and press ‘Enter’ or in R script and click ‘Run’.

 

Basic Computations in R

Let’s begin with basics. To get familiar with R coding environment, start with some basic calculations. R console can be used as an interactive calculator too. Type the following in your console:

> 2 + 3
> 5 

> 6 / 3
>  2

> (3*8)/(2*3)
> 4 

> log(12)
> 1.07

> sqrt (121)
> 11

But, what if you have done too many calculations ? It would be too painful to scroll through every command and find it out. In such situations, creating variable is a helpful way.

In R, you can create a variable using <- or = sign. Let’s say I want to create a variable x to compute the sum of 7 and 8. I’ll write it as:

> x <- 8 + 7
> x
> 15

or we can do like this

> y <- 3

> z <- 5

> x <- y+z

> x

>8

Once we create a variable, you no longer get the output directly (like calculator), unless you call the variable in the next line. Remember, variables can be alphabets, alphanumeric but not numeric. You can’t create numeric variables.(Valid variables a, a2, a_ a- , Invalid variables 1,1a)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s