Introduction To R - Part1
Introduction To R - Part1
R - Overview
• R is a programming language and software environment for statistical
analysis, graphics representation and reporting.
• The core of allows branching and looping as well as modular
programming using functions.
• R allows integration with the procedures written in the C, C++, Java, or
Python.
• R is freely available.
Features of R
• R is a well-developed, simple and effective programming language
which includes conditionals, loops, user defined recursive functions
and input and output facilities.
• R has an effective data handling and storage facility.
• R provides operators for calculations on arrays, lists, vectors and
matrices.
• R provides a large collection of tools for data analysis.
• R provides graphical facilities for data analysis and display.
Local Environment Setup
• Download R-core.exe file:
• https://cran.r-project.org/bin/windows/base/
• Download Rstudio:
• https://www.rstudio.com/products/rstudio/download/
R - Data Types
Data Type Example Code
Logical TRUE, FALSE v<-TRUE
print(class(v))
[1] "logical"
Example Output
# Create a vector. [1] "red" "green" "yellow"
apple <- c('red','green',"yellow") [1] "character"
print(apple)
# Get the class of the vector.
print(class(apple))
R - Data Types (Lists)
• A list: an R-object which can contain many
different types of elements inside it like vectors,
functions and even another list inside it.
Example Output
# Create a list. [[1]]
list1 <- list(c(2,5,3),21.3,sin) [1] 2 5 3
# Print the list. [[2]]
print(list1) [1] 21.3
[[3]]
function (x) .Primitive("sin")
R - Data Types (Matries)
• A matrix: is a two-dimensional rectangular data set.
• It can be created using a vector input to the matrix function.
Example Output
# Create a matrix. [,1] [,2] [,3]
M = matrix( c('a','a','b','c','b','a'), nrow = [1,] "a" "a" "b"
2, ncol = 3, byrow = TRUE) [2,] "c" "b" "a"
print(M)
R - Data Types (Arrays)
• Arrays: can be of any number of dimensions.
• dim attribute which creates the required number of dimension.
Example Output
# Create an array with two elements ,,1
which are 3x3 matrices each. [,1] [,2] [,3]
[1,] "green" "yellow" "green"
a <- array(c('green','yellow'),dim = [2,] "yellow" "green" "yellow"
c(3,3,2)) [3,] "green" "yellow" "green"
print(a) ,,2
[,1] [,2] [,3]
[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"
R - Data Types (Factors)
• Factor: stores the vector with distinct values of the elements.
• Factors are created using the factor() function.
• The nlevels functions gives the count of levels.
Example Output
# Create a vector. [1] green green yellow red red red
apple_colors <- green
c('green','green','yellow','red','red','red',' Levels: green red yellow
green') [1] 3
# Create a factor object.
factor_apple <- factor(apple_colors)
# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))
R - Data Types (Data Frames)
• Data frames: are tabular data objects.
• Unlike a matrix in data frame each column can contain different modes of data.
• The first column can be numeric while the second column can be character and
third column can be logical.
• It is a list of vectors of equal length.
Example Output
# Create the data frame. gender height weight Age
BMI <- data.frame( 1 Male 152.0 81 42
gender = c("Male", "Male","Female"), 2 Male 171.5 93 38
height = c(152, 171.5, 165), 3 Female 165.0 78 26
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)
R - Variables
• A variable in R can store an atomic vector, group of atomic vectors or
a combination of many R objects.
• A valid variable name consists of letters, numbers and the dot or
underline characters.
• The variable name starts with a letter or the dot not followed by a
number.
Variable Assignment
• The cat() function combines multiple items into a continuous print output.
Example Output
# Assignment using equal operator. [1] 0 1 2 3
var.1 = c(0,1,2,3) var.1 is 0 1 2 3
# Assignment using leftward operator. var.2 is learn R
var.2 <- c("learn","R") var.3 is 1 1
# Assignment using rightward operator.
c(TRUE,1) -> var.3
print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")
• Note − The vector c(TRUE,1) has a mix of logical and numeric class. So logical class is coerced to
numeric class making TRUE as 1.
R - Operators
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
• R language is rich in built-in operators and provides following types of
operators.
• Types of Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Miscellaneous Operators
Arithmetic Operators
Operator Description Example
+ Adds two vectors v <- c(2, 5.5, 6) t <- c(8, 3, 4)
print(v+t)
[1] 10.0 8.5 10.0
%% Give the remainder of the first vector v <- c( 2,5.5,6) t <- c(8, 3, 4)
with the second print(v%%t )
[1] 2.0 2.5 2.0
%/% The result of division of first vector v <- c(2, 5.5, 6) t <- c(8, 3, 4)
with second (quotient) print(v%/%t)
[1] 0 1 1
Arithmetic Operators
Operator Description Example
^ The first vector raised to the exponent v <- c( 2,5.5,6) t <- c(8, 3, 4)
of second vector print(v^t)
[1] 256.000 166.375 1296.000
Relational Operators
• Each element of the first vector is compared with the corresponding
element of the second vector.
• The result of comparison is a Boolean value.
Operator Description Example
> v <- c(2,5.5,6,9) t <- c(8,2.5,14,9)
Checks if each element of the first print(v>t)
vector is greater than the [1] FALSE TRUE FALSE FALSE
corresponding element of the
second vector.
| v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
Element-wise Logical OR operator. print(v|t)
[1] TRUE FALSE TRUE TRUE
! v <- c(3,0,TRUE,2+2i)
It is called Logical NOT operator. print(!v)
[1] FALSE TRUE FALSE FALSE
Assignment Operators
• These operators are used to assign values to vectors.
Operator Description Example
Called Left Assignment v1 <- c(3,1,TRUE,2+3i)
<− v2 <<- c(3,1,TRUE,2+3i)
or v3 = c(3,1,TRUE,2+3i)
= print(v1) print(v2) print(v3)
or [1] 3+0i 1+0i 1+0i 2+3i
<<− [1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
34