1
R Programming Language:
R programming is written by Robert Gentleman and Ross Ihaka is a language for statistical computing
and graphics.
R programming is commonly used to solve Statistics, Time series, Classification, and other Data science
tasks.
You can become a Data Scientist, Statistician, Data Analyst, R programmer, Business Analyst.
What in it for you:
Variable and Data Types
Logical operators
Vectors, Matrix, List and Data frames
Functions and Flow Control Statements
Dplyr and Tidyr Data Manipulation
Ggplot library for Data Visualization
Time series in R
What is R programming Language?
R is an open-source programming language used for statistical computing. It is one of the most popular
programming languages today. R was inspired by S+, it is similar to the S programming language.
Features in R:
It is open-source programming language. Hence, you can install R for free.
Non coders can also understand and perform programming in R as it is easy to understand.
R has various data structures and operators. It can be integrated with other programming
languages like C, C++, Java, and Python.
R consists of various inbuilt packages. This makes reporting the results of an analysis easier by
using R.
Variables in R:
Variables are used to store data with the named locations that your programs can manipulate.
A variable name can be combination of letters, digits, period and underscore.
R is case sensitive.
Valid variables
Valid Invalid variables Examples
variables
total tot@l >x<-10
sum Sum >.y<-20
.count _count ><-x+.y
.count.total TRUE >print (z)
Var .Oar [1] 30
2
Data Types in R:
Logical: TRUE, FALSE
Numeric: 10.5,7,845
Integer: 3L,40L, 4L
Complex: 3+2i
Character: ‘a’, ‘hello’, ‘13.5’
Raw: ‘Hello’ is stored as 48 65 6c 6c 6f
<-: Assignment operator
Logical Operators:
And - & = Return TRUE if both the conditions are true, else return FALSE.
Example: > 10>20 & 10<20
[1] FALSE
Or - | = Returns TRUE if any one of the conditions results in TRUE.
Examples: > 10>20 | 10<20
[1] TRUE
Not -! = Takes each element of the vector and gives the opposite logical value.
Example: > !(10 == 3)
[1] TRUE
Athematic operators:
Addition
Subtraction
Multiplication
Division
Remainder/Modulus
Exponent
3
Order of operators:
Parenthesis ()
Exponents (^)
Multi and div (left to right)
Add and sub (left to right)
Rational/Logical operators:
greater than
less than
less than/equal to
equal to
not equal
Print Formatting:
R uses the print () function to display the variables.
For Example: > x <- 10
>print(x)
[1] 10
R uses the paste () and paste0() function to format strings and variables together for printing in a few
different ways.
>print (paste (‘hello’, ‘world’))
[1] “hello world”
>print (paste (‘hello’, ‘world’, sep = ‘- ‘))
[1] “hello- world”
>paste0 (‘hello’, ‘world’) ( Does not allow space)
[1] “helloworld”
>paste0 (‘welcome’, ‘to’, ‘R’)
[1] “welcome”
4
#Basic type of R object is a vector
#Empty vectors can be created with vector () function, A vector can contain objects of same type/class.
#Note list is a vector which can contain objects of different classes.
Vectors:
Vectors: A vector is a sequence of data elements of the same basic type.
We use the c () function to declare a vector.
Example:
v1<- c (1,2,3,4,5)
v2<- c ('red', 'green', ‘yellow')
print (v1) output = [1] 1 2 3 4 5
print (v2) = [1] “red” “green” “yellow”
class (v1) = [1] “numeric”
#Numbers (numeric objects e.g. double precision real numbers)
#numbers such as 1 or 2etc (Thought at integer and represented as numeric objects behind the scenes
such as 1.00 or 2.00)
a <- 1 #numeric object
ai <1L #integer object
#Inf: represents Infinity
1/Inf
PNAN: missing value
List:
List: A list is a generic vector that can contain object of different types.
We use the list () function to create a data frame.
Example:
list_1<-list (x=c (10, 20, 30),
y =c ("a", "b", "c"),
z=c (TRUE, FALSE))
print(list_1)
5
Output:
> list_1
$x
[1] 10 20 30
Sy
[1] "a" "b" "c"
$z
[1] TRUE FALSE