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

R programming

Statistics for Engineers - L4+L5+L6

S. Devi Yamini

July 13, 2020

Lab - I July 13, 2020 1 / 23


Overview

1 Syllabus

Lab - I July 13, 2020 2 / 23


Overview

1 Syllabus

2 Evaluation Rubrics

Lab - I July 13, 2020 2 / 23


Overview

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

Lab - I July 13, 2020 2 / 23


Overview

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

Lab - I July 13, 2020 2 / 23


Overview

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

5 Data types

Lab - I July 13, 2020 2 / 23


Overview

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

5 Data types

6 Data Structures

Lab - I July 13, 2020 2 / 23


Outline

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

5 Data types

6 Data Structures

Lab - I July 13, 2020 3 / 23


Syllabus

1 Introduction to R and basic commands


2 Computation of Tables and Graphs- Summary Statistics
3 Random Variables and Probability distribution
4 Discrete and Continuous Probability distributions
5 Correlation and Regression
6 Multiple Linear Regression
7 Testing of Hypothesis-I
8 Testing of Hypothesis-II
9 Completely randomized design
10 Randomized block design

Lab - I July 13, 2020 4 / 23


Outline

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

5 Data types

6 Data Structures

Lab - I July 13, 2020 5 / 23


Evaluation Rubrics

Criteria Marks
10 labs (each lab submission carries 5 marks) 50 marks
10 labs (assessment in each lab carries 5 marks) 50 marks
Mid sem 20 marks
Total marks for internals 120

Lab - I July 13, 2020 6 / 23


Evaluation Rubrics

Criteria Marks
10 labs (each lab submission carries 5 marks) 50 marks
10 labs (assessment in each lab carries 5 marks) 50 marks
Mid sem 20 marks
Total marks for internals 120

Criteria Weightage
Internals 60%
FAT 40%
Total 100%

Lab - I July 13, 2020 6 / 23


Outline

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

5 Data types

6 Data Structures

Lab - I July 13, 2020 7 / 23


Introduction to R

A programming language and a software environment for statistical


analysis, graphics representation and reporting. It runs on a variety of
platforms

Lab - I July 13, 2020 8 / 23


Introduction to R

A programming language and a software environment for statistical


analysis, graphics representation and reporting. It runs on a variety of
platforms
Created by Ross Ihaka and Robert Gentleman in the year 1993

Lab - I July 13, 2020 8 / 23


Introduction to R

A programming language and a software environment for statistical


analysis, graphics representation and reporting. It runs on a variety of
platforms
Created by Ross Ihaka and Robert Gentleman in the year 1993
R allows integration with the procedures written in C, C++, .Net,
Python or FORTRAN languages for efficiency

Lab - I July 13, 2020 8 / 23


Introduction to R

A programming language and a software environment for statistical


analysis, graphics representation and reporting. It runs on a variety of
platforms
Created by Ross Ihaka and Robert Gentleman in the year 1993
R allows integration with the procedures written in C, C++, .Net,
Python or FORTRAN languages for efficiency
R is freely available, well developed, simple and effective programming
language which includes conditionals, loops, user defined recursive
functions and input/output facilities

Lab - I July 13, 2020 8 / 23


Introduction to R

A programming language and a software environment for statistical


analysis, graphics representation and reporting. It runs on a variety of
platforms
Created by Ross Ihaka and Robert Gentleman in the year 1993
R allows integration with the procedures written in C, C++, .Net,
Python or FORTRAN languages for efficiency
R is freely available, well developed, simple and effective programming
language which includes conditionals, loops, user defined recursive
functions and input/output facilities
R has an effective data handling and storage facility

Lab - I July 13, 2020 8 / 23


Introduction to R

A programming language and a software environment for statistical


analysis, graphics representation and reporting. It runs on a variety of
platforms
Created by Ross Ihaka and Robert Gentleman in the year 1993
R allows integration with the procedures written in C, C++, .Net,
Python or FORTRAN languages for efficiency
R is freely available, well developed, simple and effective programming
language which includes conditionals, loops, user defined recursive
functions and input/output facilities
R has an effective data handling and storage facility
It’s the no.1 choice of data scientists

Lab - I July 13, 2020 8 / 23


Outline

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

5 Data types

6 Data Structures

Lab - I July 13, 2020 9 / 23


Basics

Aim
Understanding basic concepts in R, Data types, importing/exporting data

Lab - I July 13, 2020 10 / 23


Basics

Aim
Understanding basic concepts in R, Data types, importing/exporting data

Try these!!
4+7

Lab - I July 13, 2020 10 / 23


Basics

Aim
Understanding basic concepts in R, Data types, importing/exporting data

Try these!!
4+7
52

Lab - I July 13, 2020 10 / 23


Basics

Aim
Understanding basic concepts in R, Data types, importing/exporting data

Try these!!
4+7
52
4+2∗4

Lab - I July 13, 2020 10 / 23


Basics

Aim
Understanding basic concepts in R, Data types, importing/exporting data

Try these!!
4+7
52
4+2∗4
8/2

Lab - I July 13, 2020 10 / 23


Basics

Aim
Understanding basic concepts in R, Data types, importing/exporting data

Try these!!
4+7
52
4+2∗4
8/2
7%%2

Lab - I July 13, 2020 10 / 23


Basics

Aim
Understanding basic concepts in R, Data types, importing/exporting data

Try these!!
4+7
52
4+2∗4
8/2
7%%2
5.5%/%3

Lab - I July 13, 2020 10 / 23


Basics

Aim
Understanding basic concepts in R, Data types, importing/exporting data

Try these!!
4+7
52
4+2∗4
8/2
7%%2
5.5%/%3
5 > 2, 5 < 2, 5 == 2, 5 <= 2,5 >= 2, 5! = 2

Lab - I July 13, 2020 10 / 23


Basics

Assignment
Example
x = 20
x < −20
20− > x
x = x + 10

Lab - I July 13, 2020 11 / 23


Basics

Assignment
Example
x = 20
x < −20
20− > x
x = x + 10

To generate a sequence of integers


Example
1 : 10
5 : −5

Lab - I July 13, 2020 11 / 23


Basics

NA is used to indicate that a value is missing or not available.


Example
1 + sin(NA)
1/0
sqrt(−1)

Lab - I July 13, 2020 12 / 23


Basics

NA is used to indicate that a value is missing or not available.


Example
1 + sin(NA)
1/0
sqrt(−1)

Built-in functions
Example
log (10)
log 10(10)
sin(1)
atan(1)
sqrt(2)
pi

Lab - I July 13, 2020 12 / 23


Outline

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

5 Data types

6 Data Structures

Lab - I July 13, 2020 13 / 23


Data types

Logical
Example
v=TRUE
class(v)
(or) typeof(v)
Output: “logical”

Lab - I July 13, 2020 14 / 23


Data types

Logical
Example
v=TRUE
class(v)
(or) typeof(v)
Output: “logical”

Numeric ( Decimal values. It is the default computational data type)

Example
v=16.5
class(v)
Output: “numeric”

Lab - I July 13, 2020 14 / 23


Data types

Integer
Example
v=as.integer(4)
class(v)
Output: “integer”

Lab - I July 13, 2020 15 / 23


Data types

Integer
Example
v=as.integer(4)
class(v)
Output: “integer”

Complex
Example
v=3+2i
class(v)
Output: “complex”

Lab - I July 13, 2020 15 / 23


Data types

Character
Example
v=“TRUE”
print(class(v))
Output: “character”

Lab - I July 13, 2020 16 / 23


Data types

Character
Example
v=“TRUE”
print(class(v))
Output: “character”
paste(“First”,“Second”,“Third”)
paste(“First”,“Second”,“Third”,sep=“:”)
(or) Fname=“Nathan”
Lname=“Muthuswamy”
paste(Fname,Lname,sep=“:”)

Lab - I July 13, 2020 16 / 23


Outline

1 Syllabus

2 Evaluation Rubrics

3 Introduction to R

4 Basics

5 Data types

6 Data Structures

Lab - I July 13, 2020 17 / 23


Data Structures

Unlike languages like C and Java, R assoicates each variable with


R-Objects and data type of the R-object/Data structures becomes the
data type of the variable.

Lab - I July 13, 2020 18 / 23


Data Structures

Unlike languages like C and Java, R assoicates each variable with


R-Objects and data type of the R-object/Data structures becomes the
data type of the variable.
Data structures
Vectors
Lists
Matrices
Arrays
Factors
Data Frames

Lab - I July 13, 2020 18 / 23


Data structures

Vector (The function c() is used to create a vector)

Example
a=c(“red”,“green”,“yellow”)
a
x=c(1,2,3)
x
length(x)

Lab - I July 13, 2020 19 / 23


Data structures

Vector (The function c() is used to create a vector)

Example
a=c(“red”,“green”,“yellow”)
a
x=c(1,2,3)
x
length(x)

List (it contains different types of elements)

Example
li=list(2,3,4,”a”,21.2,”b”,sin)
li

Lab - I July 13, 2020 19 / 23


Data structures

Matrices (two-dimensional rectangular data set)

Example
M=matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
M

Lab - I July 13, 2020 20 / 23


Data structures

Matrices (two-dimensional rectangular data set)

Example
M=matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
M
Arrays (Unlike matrices, arrays can be of any number of dimensions)

Example
a=array(c(”G”,”Y”),dim=c(3,3,2))
a

Lab - I July 13, 2020 20 / 23


Data structures

Factors (It stores the vector along with the distinct values of the
elements in the vector as labels)

Example
cols=c(“green”,“green”,“yellow”,“red”,“red”,“red”,“green”)
faccols=factor(cols)

Lab - I July 13, 2020 21 / 23


Data structures

Factors (It stores the vector along with the distinct values of the
elements in the vector as labels)

Example
cols=c(“green”,“green”,“yellow”,“red”,“red”,“red”,“green”)
faccols=factor(cols)

Data frames (Very important in R. A special type of list where every


element of the list has same length.)

Example
d=data.frame(id=c(’a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’),x=1:10, y=11:20)
d
b=data.frame(gender=c(“Male”,“Male”,“Female”),
height=c(152,171.5,165), weight=c(81,93,78), age=c(42,38,26))
b

Lab - I July 13, 2020 21 / 23


Data frames

Example
v = c(2, 5.5, 6)
t = c(8, 3, 4)
v + t; v − t; v ∗ t; v /t
v %%t;
v > t; v < t; v == t; v ! = t

Lab - I July 13, 2020 22 / 23


Data frames

Example
v = c(2, 5.5, 6)
t = c(8, 3, 4)
v + t; v − t; v ∗ t; v /t
v %%t;
v > t; v < t; v == t; v ! = t

Example
mtcars - Built-in data frame in R
mtcars[1, 2]
nrow (mtcars)
ncol(mtcars)
head(mtcars)

Lab - I July 13, 2020 22 / 23


Data import

Enter the following data in Excel sheet and save it as CSV file.

col1 col2 col3


34 23 34
47 65 39
98 37 29
20 48 59
38 28 56

mydata = read.csv (file.choose())

Lab - I July 13, 2020 23 / 23

You might also like