Maths Record Output .
Maths Record Output .
No:
Name :
Install Packages
>install.packages("package name")
>install.packages("ggplot2")
Loading Packages
>library(packagename)
Reg. No:
Name :
Basics of R Programming
Assignment Operator
# assignment x <- 3
>x <- value
>x = value
Evaluation
# evaluation x
## [1] 3
Case Sensitivity
>x <- 1
>y <- 3
>z <- 4
>x * y * z
[1] 12
>x * Y * z
## Error in eval(expr, envir, enclos): object 'Y' not found
# Program make a simple calculator that can add, subtract, multiply and
divide using functions
add <- function(x, y) {
return(x + y) }
subtract <- function(x, y)
{return(x - y)
}
multiply <- function(x, y) {return(x * y)
}
divide <- function(x, y) {return(x / y)
}
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = as.integer(readline(prompt="Enter choice[1/2/3/4]: "))
num1 = as.integer(readline(prompt="Enter first number: "))
num2 = as.integer(readline(prompt="Enter second number: "))
operator <- switch(choice,"+","-","*","/")
result <- switch(choice, add(num1, num2), subtract(num1, num2), multiply(num1, num2),
divide(num1, num2))
print(paste(num1, operator, num2, "=", result)
OUTPUT
[1] "Select operation."
[1] "1.Add"
[1] "2.Subtract"
[1] "3.Multiply"
[1]"4.Divide"
Enter choice[1/2/3/4]: 4
Enter first number: 20
Enter second number: 4
[1] "20 / 4 = 5"
Reg. No:
Name :
Add-on Packages
Install BSDA for Unit I by using the command
install.packages("BSDA")
Note:
Every time before applying the commands of respective program call the library
function by using the command such as library(BSDA), library(dplyr), library(qcc) etc.
Programming using R
Problem 2.1:
Compute the z score to test whether the sample data 88, 92, 94, 94, 96, 97, 97,
97, 99,99, 83, 92, 94, 94, 96, 97, 97, 97, 99, 99, 105, 109, 109, 109, 110, 112,94,
96, 97, 97, 97, 99,99, 83, 92, 94, 94, 96, 97, 97, 97, 99, 99, 105, 109, 109, 109,
110, 112, 112, 113, 114, 115 support the administrator’s belief that the mean
intelligence test score for all freshman is greater than 109 with sd 10.33.
R command:
>> library(BSDA)
>> data=c(88, 92, 94, 94, 96, 97, 97, 97, 99,99,
+83, 92, 94, 94, 96, 97, 97, 97, 99, 99, 105,
Out Put
One-sample z-Test
97.47738 NA
sample estimates:
mean of x 99.81132
Reg. No:
Name :
Problem 2.2:
Compute the z score to test whether the sample data 101, 103, 112,
102, 98, 97, 93, 105, 100, 97, 107, 93, 94, 97, 97, 100, 110, 106, 110, 103, 99,
93, 98, 106, 100, 112, 105, 100, 114, 97, 110, 102, 98, 112, 99 can be drawn
from a normal population with mean 102 and SD 6.
R command:
>> library(BSDA)
>> data=c(101, 103, 112, 102, 98, 97, 93, 105, 100, 97, 107, 93, 94, 97, 97, 100, 110, 106,
110, 103, 99, 93, 98, 106, 100, 112, 105, 100, 114, 97, 110, 102, 98, 112, 99)
Out Put
One-sample z-Test
data: data
z = 0, p-value = 1
alternative hypothesis: true mean is not equal to 102
95 percent confidence interval:
100.0122 103.9878
sample estimates:
mean of x
102
Reg. No:
Name :
Problem 3.1:
A random sample of 10 boys had the following I. Q’s: 70,120,110,101,88,83,95,98,107,100.
Do these data support the assumption of a population mean I.Q of 100? Find a reasonable range in
Programming command:
> library(BSDA)
> x=c(70,120,110,101,88,83,95,98,107,100)
Output:
data: x
86.98934 107.41066
sample estimates:
mean of x
97.2
Reg. No:
Name :
Problem 3. 2:
The marks obtained by a group of 9 regular course students and another group of 11
part-time course students in a test are given below.
Regular 56 62 63 54 60 51 67 69 58
Part-time 62 70 71 62 60 56 75 64 72 68 66
Perform a hypothesis test whether the marks obtained by regular students and part-time students differ
significantly at 5%.
Programming command:
> regular<-c (56, 62, 63, 54, 60, 51, 67, 69, 58)
> part.time<-c (62, 70, 71, 62, 60, 56, 75, 64, 72, 68, 66)
> t.test(x=regular,y=part.time,alternative =
"two.sided",conf.level=0.95,var.equal = TRUE)
sample estimates:
mean of x mean of y
60 66
Reg. No:
Name :
Problem 1:
Compute the one way analysis of variance table for the following data
A B C D
20 25 24 23
19 23 20 20
21 21 22 20
Programming command:
> table_stacked<-stack(table[,1:4])
> summary(anova)
OUTPUT :
Problem 2:
Compute the analysis of variance table to test the difference among the sample means at three
positions are significant.
Position 1 90 82 79 98 83 91
Position 3 83 89 80 94
Programming command:
> table_stacked<-stack(table[,1:3])
> summary(anova)
OUTPUT:
Problem 1:
Compute the two-way analysis of variance table for the following data
Salesmen
A B C D
Summer 45 40 38 37
Season
Winter 43 41 45 38
Monsoon 39 39 41 41
Programming command:
sales<-c(45,40,38,37,43,41,45,38,39,39,41,41)
season<-c("A","A","A","A","B","B","B","B","C","C","C","C")
class(season)
[1] "character"
season<-factor(season,levels = c("A","B","C","D"))
salesmen<-c("1","2","3","4","1","2","3","4","1","2","3","4")
salesmen<-factor(salesmen,levels = c("1","2","3","4"))
anova2way<-aov(sales~season+salesmen)
summary(anova2way)
OUTPUT:
Df Sum Sq Mean Sq F value Pr(>F)
season 2 8.17 4.083 0.535 0.611
salesmen 3 22.92 7.639 1.000 0.455
Residuals 6 45.83 7.639
Reg. No:
Name :
Problem 2:
Compute the two way analysis of variance table for the following data
Machine type
A B C D
W1 44 38 47 36
Workers
W2 46 40 52 43
W3 34 36 44 32
W4 43 38 46 33
W5 38 42 49 39
Programming command:
Output:
Reg. No:
Name :
Sample No 1 2 3 4 5 6 7 8 9 10 11 12
1 1.4 1.6 2.9 1.7 2.6 2.3 1.9 1.7 1.8 0.8 2.0
Measurements
in (mm)
1.4 2.3 1.0 2.0 3.6 2.8 2.1 1.6 2.2 2.0 1.5 2.5
1.3 2.8 1.5 0.5 2.5 3.2 2.1 1.8 1.9 1.5 2.1 1.6
1 2.7 2.0 2.2 1.8 1.5 1.7 1.4 1.2 2.0 0.9 1.8
Programming command:
library(qcc)
chartdata2<-read.table(header=FALSE,text="
+1 1.4 1.3 1
+1.4 2.3 2.8 2.7
+1.6 1 1.5 2
+2.9 2 0.5 2.2
+1.7 3.6 2.5 1.8
+2.6 2.8 3.2 1.5
+2.3 2.1 2.1 1.7
+1.9 1.6 1.8 1.4
+1.7 2.2 1.9 1.2
+1.8 2 1.5 2
+0.8 1.5 2.1 0.9
+2 2.5 1.6 1.8")
xbar_chart2<-qcc(data=chartdata2, type="xbar",sizes=4,title="X-Bar Chart",
digits=2, plot=TRUE)
OUTPUT
> library(qcc)
> chartdata2<-read.table(header=FALSE,text="
+ +10 10.2 11.3 12.4
+ +10.3 10.9 10.7 11.7
+ +11.5 10.7 11.4 12.4
+ +11 11.1 10.7 11.4
+ +11.3 11.6 11.9 12.1
+ +10.7 11.4 10.7 11
+ +11.3 11.4 11.1 10.3
+ +12.3 12.1 12.7 10.7
+ +11 13.1 13.1 12.4
+ +11.3 12.1 10.7 11.5
+ +12.5 11.9 11.8 11.3
+ +11.9 12.1 11.6 11.4
Reg. No:
Name :
Problem 2:
Plot 𝑋̅ chart for the following weights of 15 samples of 4 boxes drawn randomly.
Sample No 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Box 1 10.0 10.3 11.5 11.0 11.3 10.7 11.3 12.3 11.0 11.3 12.5 11.9 12.1 11.9 10.6
Box 2 10.2 10.9 10.7 11.1 11.6 11.4 11.4 12.1 13.1 12.1 11.9 12.1 11.1 12.1 11.9
Box 3 11.3 10.7 11.4 10.7 11.9 10.7 11.1 12.7 13.1 10.7 11.8 11.6 12.1 13.1 11.7
Box 4 12.4 11.7 12.4 11.4 12.1 11.0 10.3 10.7 12.4 11.5 11.3 11.4 11.7 12.0 12.1
Programming command:
library(qcc)
chartdata<-read.table(header=FALSE,text="
+10 10.2 11.3 12.4
+10.3 10.9 10.7 11.7
+11.5 10.7 11.4 12.4
+11 11.1 10.7 11.4
+11.3 11.6 11.9 12.1
+10.7 11.4 10.7 11
+11.3 11.4 11.1 10.3
+12.3 12.1 12.7 10.7
+11 13.1 13.1 12.4
+11.3 12.1 10.7 11.5
+12.5 11.9 11.8 11.3
+11.9 12.1 11.6 11.4
+12.1 11.1 12.1 11.7
+11.9 12.1 13.1 12
+10.6 11.9 11.7 12.1")
xbar_chart2<-qcc(data=chartdata, type="xbar",sizes=4,title="X-Bar Chart",
digits=4, plot=TRUE)
xbar_chart2
OUTPUT
> library(qcc)
> chartdata<-read.table(header=FALSE,text="
+ +10 10.2 11.3 12.4
+ +10.3 10.9 10.7 11.7
Reg. No:
Name :
Problem 1:
Plot R chart for the following weights of 15 samples of 4 boxes drawn randomly.
Sample No 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Box 1 10.0 10.3 11.5 11.0 11.3 10.7 11.3 12.3 11.0 11.3 12.5 11.9 12.1 11.9 10.6
Box 2 10.2 10.9 10.7 11.1 11.6 11.4 11.4 12.1 13.1 12.1 11.9 12.1 11.1 12.1 11.9
Box 3 11.3 10.7 11.4 10.7 11.9 10.7 11.1 12.7 13.1 10.7 11.8 11.6 12.1 13.1 11.7
Box 4 12.4 11.7 12.4 11.4 12.1 11.0 10.3 10.7 12.4 11.5 11.3 11.4 11.7 12.0 12.1
Programming command:
library(qcc)
chartdata<-read.table(header=FALSE,text="
+10 10.2 11.3 12.4
+10.3 10.9 10.7 11.7
+11.5 10.7 11.4 12.4
+11 11.1 10.7 11.4
+11.3 11.6 11.9 12.1
+10.7 11.4 10.7 11
+11.3 11.4 11.1 10.3
+12.3 12.1 12.7 10.7
+11 13.1 13.1 12.4
+11.3 12.1 10.7 11.5
+12.5 11.9 11.8 11.3
+11.9 12.1 11.6 11.4
+12.1 11.1 12.1 11.7
+11.9 12.1 13.1 12
+10.6 11.9 11.7 12.1")
R_chart<-qcc(data=chartdata, type="R",sizes=4,title="R-Chart",
digits=4, plot=TRUE)
R_chart
Reg. No:
Name :
OUTPUT
> library(qcc)
> chartdata<-read.table(header=FALSE,text="
+ +10 10.2 11.3 12.4
+ +10.3 10.9 10.7 11.7
+ +11.5 10.7 11.4 12.4
+ +11 11.1 10.7 11.4
+ +11.3 11.6 11.9 12.1
+ +10.7 11.4 10.7 11
+ +11.3 11.4 11.1 10.3
+ +12.3 12.1 12.7 10.7
+ +11 13.1 13.1 12.4
+ +11.3 12.1 10.7 11.5
+ +12.5 11.9 11.8 11.3
+ +11.9 12.1 11.6 11.4
+ +12.1 11.1 12.1 11.7
+ +11.9 12.1 13.1 12
+ +10.6 11.9 11.7 12.1")
> R_chart<-qcc(data=chartdata, type="R",sizes=4,title="R-Chart",
+ digits=4, plot=TRUE)
> R_chart
List of 11
$ call : language qcc(data = chartdata, type = "R", sizes = 4, plot
= TRUE, title = "R-Chart", digits = 4)
$ type : chr "R"
$ data.name : chr "chartdata"
$ data : num [1:15, 1:4] 10 10.3 11.5 11 11.3 10.7 11.3 12.3 11 11.3
...
..- attr(*, "dimnames")=List of 2
$ statistics: Named num [1:15] 2.4 1.4 1.7 0.7 0.8 ...
..- attr(*, "names")= chr [1:15] "1" "2" "3" "4" ...
$ sizes : num [1:15] 4 4 4 4 4 4 4 4 4 4 ...
$ center : num 1.33
$ std.dev : num 0.644
$ nsigmas : num 3
$ limits : num [1, 1:2] 0 3.03
..- attr(*, "dimnames")=List of 2
$ violations:List of 2
- attr(*, "class")= chr "qcc"
Reg. No:
Name :
Problem 2:
Sample No 1 2 3 4 5 6 7 8 9 10 11 12
1 1.4 1.6 2.9 1.7 2.6 2.3 1.9 1.7 1.8 0.8 2.0
Measurements
in (mm)
1.4 2.3 1.0 2.0 3.6 2.8 2.1 1.6 2.2 2.0 1.5 2.5
1.3 2.8 1.5 0.5 2.5 3.2 2.1 1.8 1.9 1.5 2.1 1.6
1 2.7 2.0 2.2 1.8 1.5 1.7 1.4 1.2 2.0 0.9 1.8
Programming command:
library(qcc)
chartdata<-read.table(header=FALSE,text="
+1 1.4 1.3 1
+1.4 2.3 2.8 2.7
+1.6 1 1.5 2
+2.9 2 0.5 2.2
+1.7 3.6 2.5 1.8
+2.6 2.8 3.2 1.5
+2.3 2.1 2.1 1.7
+1.9 1.6 1.8 1.4
+1.7 2.2 1.9 1.2
+1.8 2 1.5 2
+0.8 1.5 2.1 0.9
+2 2.5 1.6 1.8 ")
R_chart<-qcc(data=chartdata, type="R",sizes=4,title="R-Chart",
digits=2, plot=TRUE)
R_chart
OUTPUT
> library(qcc)
> chartdata<-read.table(header=FALSE,text="
+ +1 1.4 1.3 1
+ +1.4 2.3 2.8 2.7
+ +1.6 1 1.5 2
+ +2.9 2 0.5 2.2
+ +1.7 3.6 2.5 1.8
+ +2.6 2.8 3.2 1.5
+ +2.3 2.1 2.1 1.7
+ +1.9 1.6 1.8 1.4
+ +1.7 2.2 1.9 1.2
+ +1.8 2 1.5 2
+ +0.8 1.5 2.1 0.9
Reg. No:
Name :
Problem 1:
Let 𝑇: 𝑇2 → 𝑇3 be defined by (𝑇, 𝑇) = (2𝑇 − 𝑇, 3𝑇 + 4𝑇, 𝑇). Compute the null space of
𝟐 −𝟏
the matrix 𝑻 = [𝟑 𝟒]
𝟏 𝟎
Programming command:
library(pracma)
T<-matrix(c(2,-1,3,4,1,0),nrow=3,ncol=2,byrow=TRUE)
N<-nullspace(T)
OUTPUT
> library(pracma)
> T<-matrix(c(2,-1,3,4,1,0),nrow=3,ncol=2,byrow=TRUE)
> T
[,1] [,2]
[1,] 2 -1
[2,] 3 4
[3,] 1 0
> N<-nullspace(T)
> N
NULL
Reg. No:
Name :
Problem 2:
Let 𝑇: 𝑅3 → 𝑅3 be defined by (𝑥, 𝑦, 𝑧) = (𝑎, 𝑏, 0). Compute the null space of the matrix 𝑇 =
1 0 0
[0 1 0]
0 0 0
Programming command:
library(pracma)
T<-matrix(c(1,0,0,0,1,0,0,0,0),nrow=3,ncol=3,byrow=TRUE)
N<-nullspace(T)
OUTPUT
> library(pracma)
> T<-matrix(c(1,0,0,0,1,0,0,0,0),nrow=3,ncol=3,byrow=TRUE)
> T
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 0
> N<-nullspace(T)
> N
[,1]
[1,] 0
[2,] 0
[3,] 1
Reg. No:
Name :
Programming command:
library(pracma)
A<-matrix(c(3,-4,4,1,-2,4,1,-1,3),nrow=3,ncol=3)
Eigen.Values.Vectors<-eigen(A)
Eigen.Values.Vectors
OUTPUT
> library(pracma)
> A<-matrix(c(3,-4,4,1,-2,4,1,-1,3),nrow=3,ncol=3)
> A
[,1] [,2] [,3]
[1,] 3 1 1
[2,] -4 -2 -1
[3,] 4 4 3
> Eigen.Values.Vectors<-eigen(A)
> Eigen.Values.Vectors
eigen() decomposition
$values
[1] 3 2 -1
$vectors
[,1] [,2] [,3]
[1,] 0.5773503 -7.071068e-01 -6.409876e-17
[2,] -0.5773503 7.071068e-01 -7.071068e-01
[3,] 0.5773503 -5.551115e-17 7.071068e-01
Reg. No:
Name :
Problem 2:
2 2 −7
Compute the eigenvalue and eigen vector of the linear transformation 𝑇 = [2 1 2 ]
0 1 −3
Programming command:
library(pracma)
Eigen.Values.Vectors<-eigen(A)
Eigen.Values.Vectors
OUTPUT
> library(pracma)
> A<-matrix(c(2,2,-7,2,1,2,0,1,-3),nrow=3,ncol=3)
> A
[,1] [,2] [,3]
[1,] 2 2 0
[2,] 2 1 1
[3,] -7 2 -3
> Eigen.Values.Vectors<-eigen(A)
> Eigen.Values.Vectors
eigen() decomposition
$values
[1] -4 3 1
$vectors
[,1] [,2] [,3]
[1,] 0.07474351 -0.6666667 -0.4364358
[2,] -0.22423053 -0.3333333 0.2182179
[3,] 0.97166562 0.6666667 0.8728716