Introduction To R

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

SEN4103

What is R programming?

 R is an open source programming language used for statistical computing.


 It is one of the most popular PL today.
 R was inspired by S+, it is similar to S programming language
By Learning R

 Data scientist
 Statistician
 Data analyst
 R Programmer
 Business Analyst
Companies using R
Features Of R

 It is an open source PL, hence you can install R free


 Non-coders can also understand and perform programming in r
 It has various data structures and operators
 It can be integrated with other programming languages
 It consists of various inbuilt packages
How to install R
How to install R
How to install R
How to install R
Base R
RStudio
RStudio
Set the working directory in R Studio
Set the working directory in R Studio
R vs Python
Feature R Python

R is a language and
Python is a general-
environment for
purpose programming
statistical programming
Introduction language for data
which includes
analysis and scientific
statistical computing
computing
and graphics.

It can be used to
It has many features develop GUI
which are useful for applications and web
Objective
statistical analysis and applications as well as
representation. with embedded
systems

It has many easy-to- It can easily perform


Workability use packages for matrix computation as
performing tasks well as optimization

Various popular Python


Integrated Various popular R IDEs
IDEs are Spyder,
development are Rstudio, RKward, R
Eclipse+Pydev, Atom,
environment commander, etc.
etc.

Some essential
There are many
Libraries and packages and libraries
packages and libraries
packages are Pandas, Numpy, Sc
like ggplot2, caret, etc.
ipy, etc.

It takes a more
It is mainly used for
streamlined approach
Scope complex data analysis
for data science
in data science.
projects.
R vs Python
Advantages of R

 R is the most comprehensive statistical analysis package. As new


technology and concepts often appear first in R.
 As R programming language is an open source. Thus, you can run R
anywhere and at any time.
 R programming language is suitable for GNU/Linux and Windows
operating systems.
 R programming is cross-platform and runs on any operating system.
 In R, everyone is welcome to provide new packages, bug fixes, and code
enhancements.
Disadvantages of R

 In the R programming language, the standard of some packages is less


than perfect.
 Although, R commands give little pressure on memory management. So R
programming language may consume all available memory.
 In R basically, nobody to complain if something doesn’t work.
 R programming language is much slower than other programming
languages such as Python and MATLAB.
Syntax of R program

 A program in R is made up of three things:


 Variables
 Comments and
 Keywords.
Comments in R

 Single line comments can be written by using # at the beginning of the


statement
Keywords in R
R Data Types

Basic Data Types Values Examples


Numeric Set of all real numbers numeric_value <- 3.14

Integer Set of all integers, Z integer_value <- 42L

Logical TRUE and FALSE logical_value <- TRUE

Complex Set of complex numbers complex_value <- 1 + 2i

“a”, “b”, “c”, …, “@”, “#”, “$”,


Character character_value <- "Hello"
…., “1”, “2”, …etc
Variables in R

 Variables are used to store data with named locations that your programs can
manipulate
 A variable can be a combination of letters, digits, period and underscore. No other
special character is allowed
 A valid variable name consists of a combination of alphabets, numbers, dot(.), and
underscore(_) characters. Example: var.1_ is valid
 Apart from the dot and underscore operators, no other special character is allowed.
Example: var$1 or var#1 both are invalid
 Variables can start with alphabets or dot characters. Example: .var or var is valid
 The variable should not start with numbers or underscore. Example: 2var or _var is invalid.
 If a variable starts with a dot the next thing after the dot cannot be a number. Example:
.3var is invalid
 The variable name should not be a reserved keyword in R. Example: TRUE, FALSE,etc.
Variables in R

 = (Simple Assignment)
 <- (Leftward Assignment)
 -> (Rightward Assignment)
Important Methods for R Variables

 class() function
is used to determine the data type of the variable provided to it.

 ls() function
is used to know all the present variables in the workspace.

 rm() function
is used to delete an unwanted variable within your workspace.
Operators

 Arithmetic Operators
 Addition
 Substraction
 Multiplication
 Division
 Remainder / mod
 Exponent
Order of operatios

 Paranthesis
 Exponent (^)
 Multiplication and division (left to rigth)
 Addition and substractions (left to rigth)
Rational / Logical Operators

 Greater than >


 Less than <
 Greater than / equal >=
 Less than / equal <=
 Equal to ==
 Not equal !=
 And &
 OR |
 Not !
Input/Output

 There are two methods in R.


 Using readline() method
 Using scan() method
readline() method

 readline() method takes input in string format.


 E.g. If the given input is 255, R will take it as ‘255’
 To convert the inputted value to the desired data type, there are some
functions in R
 as.integer(n); —> convert to integer
 as.numeric(n); —> convert to numeric type (float, double etc)
 as.complex(n); —> convert to complex number (i.e 3+2i)
 as.Date(n) —> convert to date …, etc
Taking multiple inputs in R

 Taking multiple inputs in R language is same as taking single input, just need to
define multiple readline() for inputs.
 Syntax:
var1 = readline(“Enter 1st number : “);
var2 = readline(“Enter 2nd number : “);
var3 = readline(“Enter 3rd number : “);
var4 = readline(“Enter 4th number : “);
or,
{
var1 = readline(“Enter 1st number : “);
var2 = readline(“Enter 2nd number : “);
var3 = readline(“Enter 3rd number : “);
var4 = readline(“Enter 4th number : “);
}
Taking String and Character input in R

 In order to prompt the user, prompt keyword can be used with in the
readline method
 E.g. var1 = readline(prompt = “Enter any number : “);
 To take string input
 Syntax:
string:
var1 = readline(prompt = “Enter your name : “);
character:
var1 = readline(prompt = “Enter any character : “);
var1 = as.character(var1)
Execution of an R file

You might also like