Basic R Programming

Hello Data Experts,

Let me continue from my last blog “Guide to install R and R Studio” where I had shared features for R Studio on windows platform. Now that we are ready and familiar with R Studio, next steps are to make our-self comfortable with R Programming.

R Programming

Let me start with a statement R programing is very much like how we operate calculator. We can also call R as a glorified calculator to start with.


Few basic tips to keep in mind while working on R:

  • Comments start with “#” character.
  • No need to declare variable, assigning a value to a variable directly will work. Assignment of a value to a variable is done by left assign.
  • Please note there are 3 ways to assign a variable
    • Right Assign            25 -> b
    • Left Assign             a <- 1 (Left assign is the best practice for R programing to do value assignment).
    • Assign operator       c = 24
  • Help can be invoked by prefixing “?” character.
    • ?Foreign
  • Data Type is assigned by R internally based on the value assigned to the variable.
    • St <- “How are you”
    • Bi <- TRUE
    • Nu <- 1
  • R programming language is case sensitive, these are 2 different variables.
    • uc <- “LOWERCASE”
    • UC <- “UPPERCASE”
  • R accepts “.” Character in variables, hence no need to have variable with underscore.
    • Height.inch <- 23

BASIC OPERATORS

Let us start with Data Operators “SUM”, “SUBSTRACTION”, “MULTIPLICATION” and “DIVISION”.

First assign values to variables
FV <- 2
SV <- 6
TV <- 3

SUM:
FV + SV
# FV + SV will result 8

SUBSTRACTION:
SV – TV
# SV – TV will result 3

DIVISION:
SV/TV
# SV/TV will result 2

MULTIPLICATION:
FV*TV
# FV*TV will result 6


ADVANCED OPERATORS

Let us use advanced operator, POWER, REMAINDER and QUOTIENT

POWER
TV^FV
# TV^FV will result 9, as it will be calculated as 3 to the power 2.

REMAINDER
TV%%FV
# This will result 1

QUOTIENT > DIVISION ADJUSTED TO WHOLE LFET
TV%/%FV
# This will result 1


RELATIONAL OPERATORS

Let us star using relation operator for comparison , !=, ==, =.  We can also mix and match these operators.

a <- 4
b <- 9

a > b   # This will FALSE

a != b  # This will TRUE

a == b  # This will FALSE

a = b  # This will FALSE

a => b  # This is not the right syntax hence will error out.

I am sure this blog was useful and must have helped you understand “How to use basic Operators using R”. With this we are all set to go head and start practicing advance R using RStudio. In my next blog, I will begin with “Advanced R using R Studio”.

I had kept this session light as it is important to have basics clear. Thank you being with me for this blog I hope it was helpful. Kindly share your valuable and kind opinion. Please do not forget to suggest what you would like to understand and hear from me in my future blogs.

Thank you…
Outstanding Outliers:: “AG”.
OOPA

Advertisements

One thought on “Basic R Programming

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s