0% found this document useful (0 votes)
42 views24 pages

R Program Record Book Iba

This document provides examples of various operations in R including creating lists, vectors, matrices, data frames and arrays. It also demonstrates arithmetic operations on vectors, addition and subtraction of matrices, logical operators, and functions to view datasets. Key concepts covered are creating and manipulating different data structures, performing basic math operations, and using functions like summary(), mean(), var(), head() and tail() to explore datasets.

Uploaded by

TEJASWINI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views24 pages

R Program Record Book Iba

This document provides examples of various operations in R including creating lists, vectors, matrices, data frames and arrays. It also demonstrates arithmetic operations on vectors, addition and subtraction of matrices, logical operators, and functions to view datasets. Key concepts covered are creating and manipulating different data structures, performing basic math operations, and using functions like summary(), mean(), var(), head() and tail() to explore datasets.

Uploaded by

TEJASWINI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

INTRODUCTION TO BUSINESS ANALYTICS

RECORD BOOK

1. Creating a list:
# create a list
list1<-list(c("ice cream","donuts","black forest
cake"),c(1,2,3))
print(list1)
RESULT:
# create a list
> list1<-list(c("ice cream","donuts","black forest
cake"),c(1,2,3))
> print(list1)
[[1]]
[1] "ice cream" "donuts" "black forest cake"
[[2]]
[1] 1 2 3

2. Creating a vector
#creating vector
v1=10:20
print(v1)
v2=20:10
print(v2)
print(v1+v2)

RESULT:

#creating vector
> v1=10:20
> print(v1)
[1] 10 11 12 13 14 15 16 17 18 19 20
> v2=20:10
> print(v2)
[1] 20 19 18 17 16 15 14 13 12 11 10
> print(v1+v2)
[1] 30 30 30 30 30 30 30 30 30 30 30
3. creating matrix with dimension

#creating matrix with dimension


#create a matrix
STUDENTS<-matrix(c('A','B','B','C','B','B'),nrow = 2,ncol
= 3,byrow = TRUE)
print(STUDENTS)
#Setting Row and Column names
sdata<-matrix(c("shiva","assistant
manager","30000","ashwini","manager","60000"),nro
w = 2,ncol = 3,byrow = TRUE,
dimnames=list(c("row1","row2"),c("name","designatio
n","salary")))
sdata

RESULT:

#creating matrix with dimension


> #create a matrix
> STUDENTS<-matrix(c('A','B','B','C','B','B'),nrow =
2,ncol = 3,byrow = TRUE)
> print(STUDENTS)
[,1] [,2] [,3]
[1,] "A" "B" "B"
[2,] "C" "B" "B"
> #Setting Row and Column names
> sdata<-matrix(c("shiva","assistant
manager","30000","ashwini","manager","60000"),nro
w = 2,ncol = 3,byrow = TRUE,
+ dimnames =
list(c("row1","row2"),c("name","designation","salary"))
)
> sdata
name designation salary
row1 "shiva" "assistant manager" "30000"
row2 "ashwini" "manager" "60000"
4. Arithmetic operations

#Arthematic operations
A = c(1,3,5,7)
B = c(1,2,4,8)
#Addition
A+B
#Subtraction
A-B
#multiplication
A*B
#division
A/B
#recycling rule
U = c(10,20,30)
V = c(1,2,3,4,5,6,7,8,9)
U+V
A + c()

RESULT:

> #Arthematic operations


> A = c(1,3,5,7)
> B = c(1,2,4,8)
> #Addition
>A+B
[1] 2 5 9 15
> #Subtraction
>A-B
[1] 0 1 1 -1
> #multiplication
>A*B
[1] 1 6 20 56
> #division
>A/B
[1] 1.000 1.500 1.250 0.875
> #recycling rule
> U = c(10,20,30)
> V = c(1,2,3,4,5,6,7,8,9)
>U+V
[1] 11 22 33 14 25 36 17 28 39
> A + c()
numeric(0)
5. Addition of matrix

#Addition of matrix
A=matrix(c(5,10,15,20,25,30),nrow=3,ncol=2)
print(A)
B=matrix(c(2,4,6,8,10,12),nrow=3,ncol=2)
print(B)
#add the matrix
answer<-20+8
print(answer)
#subtraction of matrix
answer<-30-12
print(answer)
#multiplication of matrix
answer<-10*4
print(answer)
#division of matrix
answer<-30/12
print(answer)

RESULT:
#Addition of matrix
> A=matrix(c(5,10,15,20,25,30),nrow=3,ncol=2)
> print(A)
[,1] [,2]
[1,] 5 20
[2,] 10 25
[3,] 15 30
> B=matrix(c(2,4,6,8,10,12),nrow=3,ncol=2)
> print(B)
[,1] [,2]
[1,] 2 8
[2,] 4 10
[3,] 6 12
> #add the matrix
> answer<-20+8
> print(answer)
[1] 28
> #subtraction of matrix
> answer<-30-12
> print(answer)
[1] 18
> #multiplication of matrix
> answer<-10*4
> print(answer)
[1] 40
> #division of matrix
> answer<-30/12
> print(answer)
[1] 2.5
6. To get the length of text string

#to get the length of a text string


str="TEJASWINI TELUGARA"
name=nchar(str)
print(name)
#to get the length of vector containing the string
V1=c("TEJASWINI","TELUGARA")
print(V1)
nstr=length(V1)
print(nstr)
#to find min, max and range
T=c(1,3,6,9,12)
print(T)
min(T)
max(T)
range(T)

RESULT:

#to get the length of a text string


> str="TEJASWINI TELUGARA"
> name=nchar(str)
> print(name)
[1] 18
> #to get the length of vector containing the string
> V1=c("TEJASWINI","TELUGARA")
> print(V1)
[1] "TEJASWINI" "TELUGARA"
> nstr=length(V1)
> print(nstr)
[1] 2
> #to find min, max and range
> T=c(1,3,6,9,12)
> print(T)
[1] 1 3 6 9 12
> min(T)
[1] 1
> max(T)
[1] 12
> range(T)
[1] 1 12

7. Creating an array
#to create an array with two elements
a<- array(c('red','white'),dim = c(3,3,2))
print(a)
a<-array(c(1:2),dim = c(3,4))
print(a)
#to create an array of students marks
b<- array (c('student markers'), dim = c(3,2))
print(b)

RESULT:
#to create an array with two elements
> a<- array(c('red','white'),dim = c(3,3,2))
> print(a)
,,1

[,1] [,2] [,3]


[1,] "red" "white" "red"
[2,] "white" "red" "white"
[3,] "red" "white" "red"

,,2

[,1] [,2] [,3]


[1,] "white" "red" "white"
[2,] "red" "white" "red"
[3,] "white" "red" "white"

> print(a)
,,1

[,1] [,2] [,3]


[1,] "red" "white" "red"
[2,] "white" "red" "white"
[3,] "red" "white" "red"

,,2

[,1] [,2] [,3]


[1,] "white" "red" "white"
[2,] "red" "white" "red"
[3,] "white" "red" "white"

> #to create an array of students marks


> b<- array (c('student markers'), dim = c(3,2))
> print(b)
[,1] [,2]
[1,] "student markers" "student markers"
[2,] "student markers" "student markers"
[3,] "student markers" "student markers"
8. Create a Data frame

#create a data frame


INFO<-
data.frame( gender=c("Female","female","male"),
height = c(5.1,4.11,6.1), weight = c(63,50,80), age=
c(22,30,46))
print (INFO)
#create the data frame of employee details
#create a data frame of empoyee Id, employee
Name,employee Designation, address, gender,
salary
Details<-data.frame( emp_id =
c(111,112,113),emp_name=c("tejaswini","vani","vi
kram"),designation = c("Manager","Assistant
manager","HR executive"), address
=c("Ballari","shivmoga","Bnaglore"), gender =
c("female","female","male"),
salary=c(80000,50000,30000))
print(Details)

RESULT:
#create a data frame
> INFO<-
data.frame( gender=c("Female","female","male"),
+ height = c(5.1,4.11,6.1), weight = c(63,50,80),
age= c(22,30,46))
> print (INFO)
gender height weight age
1 Female 5.10 63 22
2 female 4.11 50 30
3 male 6.10 80 46
> #create the data frame of employee details
> #create a data frame of empoyee Id, employee
Name,employee Designation, address, gender,
salary
> Details<-data.frame( emp_id =
c(111,112,113),emp_name=c("tejaswini","vani","vi
kram"),designation = c("Manager","Assistant
manager","HR executive"), address
=c("Ballari","shivmoga","Bnaglore"), gender =
c("female","female","male"),
salary=c(80000,50000,30000))
> print(Details)
emp_id emp_name designation address
gender salary
1 111 tejaswini Manager Ballari female
80000
2 112 vani Assistant manager shivmoga
female 50000
3 113 vikram HR executive Bnaglore male
30000

9. OPERATORS

#AND OPERATOR
s<-c(6,8,TRUE,3+5i)
j<-c(9,43,FALSE,3+5i)
print(s&j)
#OR OPERATOR
r<-c(5,7,TRUE,3+5i)
t<-c(8,0,FALSE,3+5i)
print(r|t)
#not operator
t<-c(9,7,TRUE,3+5i)
print(!t)
#Logical operator
s<-c(6,2,TRUE,2+5i)
j<-c(2,8,FALSE,2+5i)
print(s&&j)
#Logicalor
s<-c(4,3,TRUE,4+6i)
j<-c(9,8,FALSE,4+6i)
print(s||j)
RESULT:
#AND OPERATOR
> s<-c(6,8,TRUE,3+5i)
> j<-c(9,43,FALSE,3+5i)
> print(s&j)
[1] TRUE TRUE FALSE TRUE
> #OR OPERATOR
> r<-c(5,7,TRUE,3+5i)
> t<-c(8,0,FALSE,3+5i)
> print(r|t)
[1] TRUE TRUE TRUE TRUE
> #not operator
> t<-c(9,7,TRUE,3+5i)
> print(!t)
[1] FALSE FALSE FALSE FALSE
> #Logical operator
> s<-c(6,2,TRUE,2+5i)
> j<-c(2,8,FALSE,2+5i)
> print(s&&j)
[1] TRUE
> #Logicalor
> s<-c(4,3,TRUE,4+6i)
> j<-c(9,8,FALSE,4+6i)
> print(s||j)
[1] TRUE
10. to view

data() # to view data set


View(rock) #view bike data set
summary(rock) #to view summary of bike data set
mean(rock$area) #to find the mean of the area in
rock data set
var(rock$area) #to find the variance of the area in
rock data set
head(rock) #to view first 6 rows of rock data set
tail(rock) #to view last 6 rows of rock database

RESULT:

data() # to view data set


> View(rock) #view bike data set
> summary(rock) #to view summary of bike data
set
area peri shape perm
Min. : 1016 Min. : 308.6 Min. :0.09033 Min.
: 6.30
1st Qu.: 5305 1st Qu.:1414.9 1st Qu.:0.16226
1st Qu.: 76.45
Median : 7487 Median :2536.2 Median :0.19886
Median : 130.50
Mean : 7188 Mean :2682.2 Mean :0.21811
Mean : 415.45
3rd Qu.: 8870 3rd Qu.:3989.5 3rd Qu.:0.26267
3rd Qu.: 777.50
Max. :12212 Max. :4864.2 Max. :0.46413
Max. :1300.00
> mean(rock$area) #to find the mean of the area
in rock data set
[1] 7187.729
> var(rock$area) #to find the variance of the area
in rock data set
[1] 7203045
> head(rock) #to view first 6 rows of rock data set
area peri shape perm
1 4990 2791.90 0.0903296 6.3
2 7002 3892.60 0.1486220 6.3
3 7558 3930.66 0.1833120 6.3
4 7352 3869.32 0.1170630 6.3
5 7943 3948.54 0.1224170 17.1
6 7979 4010.15 0.1670450 17.1
> tail(rock) #to view last 6 rows of rock database
area peri shape perm
43 5605 1145.690 0.464125 1300
44 8793 2280.490 0.420477 1300
45 3475 1174.110 0.200744 580
46 1651 597.808 0.262651 580
47 5514 1455.880 0.182453 580
48 9718 1485.580 0.200447 580

# test statistics - standardized value calculated from


the sample dataset
#during hypothesis testing is test statistics
#Test statistics changes from distribution to
distribution
# T-test
#analyses two populations using statistical differences
#statsistical differences between two samples
#used with small sample size]
#when the variance of the two normal distribution
#are not known
a<-c(1.1,2.3,1.5,3.4,5.4)
b<-c(1.4,2.5,1.7,2.4,4.1)
#T-Test
t.test(a,b)
install.packages("MASS")
library(MASS)
View(UScrime)
dim(UScrime)
str(UScrime)
#Independent T-test
#The independent t test is used to test if there is
#any statistically significant difference between two
means.
#Use of an independent t test requires several
assumptions to be satisfied. The assumptions are listed
below

#The variables are continuous and independent


#The variables are normally distributed
#The variances in each group are equal

#Null hypothesis = There is no difference between


southern states and non souther states
#Alternative = There is difference between states for
imprissonment
t.test(Prob~So,data=UScrime)
# deduction : Reject Null and accept Alternative
Hypothesis

#Dependent T test
#When ``two groups aren't independent
#Null - No relation between unemployemt  of group U1
and U2
#Alt- There is relation ship between unemployment of
U1 and U2
Dep<-t.test(UScrime$U1,UScrime$U2,paired = TRUE)
Dep

#F -Test
#compares the variance of any two samples from
normal distribution
#compares the Ratio of the two variables and if these
are equal , the ratio will be 1
#compares the statistical models that are fitted to
dataset
#create two normal distribution
a<-rnorm(40,mean=0)
a
b<-rnorm(20,mean=0)
b
var.test(a,b)

#Non Parametric tests


# median values

#Wilcoxon Test also known as U test


# outcome varaibles are skewed or ordinal in nature
#if two groups are independent
#independent value is dichotomous

g<-wilcox.test(Prob~So,data=UScrime)
g
# Null rejected
# More appropriate when groups are paired

#comparing more than two group

#Kruskal Wallis Test

# evaluate group differences if groups are independent

View(state.x77)
states<-data.frame(state.region,state.x77)
states
kruskal.test(Illiteracy~state.region,data=states)

#Reject Null hypothesis

#Test of Independence

#Both dependent and independent variables are


categorical
install.packages("vcd")
library(vcd)
#Chi-Square test
View(Arthritis)
mytable1<-xtabs(~Treatment+Improved,data=Arthritis)
mytable1
chisq.test(mytable1)
mytable2<-xtabs(~Improved+Sex,data=Arthritis)
mytable2
chisq.test(mytable2)

#Fishers Exact Test

#independence of row and columns

mytable3<-xtabs(~Treatment+Improved,data=Arthritis)
mytable3
fisher.test(mytable3)

# Test of Independence

#Chi-Square test
View(Arthritis)
mytable1<-xtabs(~Treatment+Improved,data=Arthritis)
chisq.test(mytable1)
mytable2<-xtabs(~Improved+Sex,data=Arthritis)
chisq.test(mytable2)
# Correlation
#Correlation coefficients are used to describe
relationships among quantitative variables. The sign ±
indicates the direction of the relationship (positive or
inverse) and
#the magnitude indicates the strength of the
relationship (ranging from 0 for no relationship to 1 for
a perfectly predictable relationship)
#PEARSON, SPEARMAN, AND KENDALL CORRELATIONS

#The Pearson product moment correlation assesses


the degree of linear relationship
#between two quantitative variables.
#Spearman's Rank Order correlation coefficient
assesses the degree of relationship between two rank-
ordered variables.
#Kendall's Tau is also a nonparametric measure of rank
correlation.
#cor(x,use=,method=)
states<-state.x77[,1:6]
View(states)
cor(states)
cor(states,method="spearman")
cor(states,method="kendall")
x <- states[,c("Population", "Income", "Illiteracy", "HS
Grad")]
y <- states[,c("Life Exp", "Murder")]
cor(x,y)

You might also like