Introduction to R programming

Hi there, welcome to week 2 session.

Today we will learn,

  1. Why did I chose R over python
  2. Introduction to R language
  3. Basics of R

Why R over python?

We can choose R or python for data analysis. If you are already familiar with python, you can go with python. But I was newbie in both technologies.

I selected R because of the following reasons.

  • R is object-oriented
  • R is a functional programming language
  • Operator overloading is much easier in R than in Python
  • Parallelism in R has been much further developed than in Python
  • R is designed for statistical analysis
  • R is great for exploratory work
  • R has huge number of packages and readily usable tests that often provide you with the necessary tools to get up and running quickly
  • R can even be part of a big data solution

Introduction to R language

R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team, of which Chambers is a member.

As you know, we need an environment to run any program. You need to have r-base to run R programs.

You can download r-base by following below links.

For Windows machine, click here

For mac OSX machine, click here 

For Linux  machine, click here

(if any of the link is broken, get the r-base from cran website)

Now we have r-base. We can start coding! But we always prefer to work with IDEs than working on command line. Even R has a beautiful IDE called RStudio.

RStudio is an open source IDE. You can download it from their website. Here is the link.

Basics of R

Hope you have installed r-base and RStudio on your machine. Now launch RStudio or r-base interface.

After R is started, there is a console awaiting for input. At the prompt (>), you can enter numbers and perform calculations.

eg:

 > 1 + 2 

output:
[1] 3 

Variable assignment

We assign values to variables with the assignment operator “=”. Just typing the variable by itself at the prompt will print out the value. We should note that another form of assignment operator “<-” is also in use. I prefer using “<-” operator, for no specific reason!

eg:

> x = 1
> x 

output:
[1] 1 

 

Comments

All text after the pound sign “#” within the same line is considered as a comment.

eg:

> 1 + 1      # this is a comment 

output:

[1] 2

Functions

R functions are invoked by its name, then followed by the parenthesis, and zero or more arguments. The following apply the function c to combine three numeric values into a vector.

eg:

> c(1, 2, 3) 

output:

[1] 1 2 3 

Extension Package

Sometimes we need additional functionality beyond those offered by the core R library. In order to install an extension package, you should invoke the install.packages function at the prompt and follow the instruction.

eg:

> install.packages(“package_name”) 

Getting Help

R provides extensive documentation. For example, entering ?c or help(c) at the prompt gives documentation of the function c in R.

eg:

> help(c) 

If you are not sure about the name of the function you are looking for, you can perform a fuzzy search with the apropos function.

eg:

> apropos(“can”)

output:

[1] “.rs.scanFiles” “canCoerce”     “cancor”        “scan”          “volcano”

I will be writing about Sentiment analysis of twitter and WhatsApp data in the next post.

Thanks for visiting my blog. I always love to hear constructive feedback. Please give your feedback in the comment section below or write to me personally here.

 

Reference:

https://cran.r-project.org/doc/manuals/R-intro.pdf

https://www.datacamp.com/courses/free-introduction-to-r

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