R Program Record Book Iba
R Program Record Book Iba
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
RESULT:
#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:
#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
RESULT:
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
,,2
> print(a)
,,1
,,2
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
RESULT:
#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)
g<-wilcox.test(Prob~So,data=UScrime)
g
# Null rejected
# More appropriate when groups are paired
View(state.x77)
states<-data.frame(state.region,state.x77)
states
kruskal.test(Illiteracy~state.region,data=states)
#Test of Independence
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