0% found this document useful (0 votes)
2 views14 pages

Model 1

The document provides a comprehensive guide on using R software for various calculations, data manipulation, and statistical analysis. It covers operations such as basic arithmetic, data frame creation, matrix operations, and statistical measures like mean, median, standard deviation, and correlation. Additionally, it includes examples of calculating confidence intervals and demonstrates how to analyze the relationship between fertilizers and crop yields.

Uploaded by

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

Model 1

The document provides a comprehensive guide on using R software for various calculations, data manipulation, and statistical analysis. It covers operations such as basic arithmetic, data frame creation, matrix operations, and statistical measures like mean, median, standard deviation, and correlation. Additionally, it includes examples of calculating confidence intervals and demonstrates how to analyze the relationship between fertilizers and crop yields.

Uploaded by

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

1.

R SOFTWARE AS A CALCULATOR

INPUT:

ls()
rm(list=ls())
45+26
43-35
63*6
110/10
3^2+6/3+2
(3^2+6)/(3+2)
a=10
b=25
c=a+b
c
a-b
a*b
a/b
date()
round(3.14879526)
exp(3)
log(exp(1))
log10(3)
sqrt(36)
abs(-89)
abs(23-56)
factorial(4)
sin(0)
sin(pi/2)
sin(30)
sin(30*pi/180)
cos(90)
cos(90*pi/180)
tan(45)
tan(45*pi/180)
cosh(30)
cosh(30*pi/180)
acos(0)
acos(cos(0))
x=c(45,12,89,78,45,69,52,47,69,44)
length(x)
sum(x)
x[1]
x[10]

OUTPUT:

> ls()
[1] "a" "b" "c" "x"
> rm(list=ls())
> 45+26
[1] 71
> 43-35
[1] 8
> 63*6
[1] 378
> 110/10
[1] 11
> 3^2+6/3+2
[1] 13
> (3^2+6)/(3+2)
[1] 3
> a=10
> b=25
> c=a+b
>c
[1] 35
> a-b
[1] -15
> a*b
[1] 250
> a/b
[1] 0.4
> date()
[1] "Fri May 6 14:11:36 2022"
> round(3.14879526)
[1] 3
> exp(3)
[1] 20.08554
> log(exp(1))
[1] 1
> log10(3)
[1] 0.4771213
> sqrt(36)
[1] 6
> abs(-89)
[1] 89
> abs(23-56)
[1] 33
> factorial(4)
[1] 24
> sin(0)
[1] 0
> sin(pi/2)
[1] 1
> sin(30)
[1] -0.9880316
> sin(30*pi/180)
[1] 0.5
> cos(90)
[1] -0.4480736
> cos(90*pi/180)
[1] 6.123234e-17
> tan(45)
[1] 1.619775
> tan(45*pi/180)
[1] 1
> cosh(30)
[1] 5.343237e+12
> cosh(30*pi/180)
[1] 1.140238
> acos(0)
[1] 1.570796
> acos(cos(0))
[1] 0
> x=c(45,12,89,78,45,69,52,47,69,44)
>length(x)
[1] 10
> sum(x)
[1] 550
> x[1]
[1] 45
> x[10]
[1] 44
2. DATA ENTRY,MANIPULATION AND RETRIEVAL

INPUT:

rm(list=ls())
Name=c("Kannan","Raju","Rajesh","Manju","Kavi")
Age=c(24,55,58,49,15)
Height=c(145,170,160,155,150)
Weight=c(45,58,69,54,45)
data1=data.frame(Name,Age,Height,Weight)
data1
Gender=c("M","M","M","F","F")
City=c("Kovai","Madurai","Erode","Chennai","Trichy")
data2=data.frame(Gender,City)
data2
data3=cbind(data1,data2)
data3
Name=c("Raji","Banu")
Age=c(24,26)
Height=c(170,156)
Weight=c(50,45)
data4=data.frame(Name,Age,Height,Weight)
data4
data5=rbind(data1,data4)
data5

OUTPUT:

> rm(list=ls())
> Name=c("Kannan","Raju","Rajesh","Manju","Kavi")
> Age=c(24,55,58,49,15)
> Height=c(145,170,160,155,150)
> Weight=c(45,58,69,54,45)
> data1=data.frame(Name,Age,Height,Weight)
> data1
Name Age Height Weight
1 Kannan 24 145 45
2 Raju 55 170 58
3 Rajesh 58 160 69
4 Manju 49 155 54
5 Kavi 15 150 45
> Gender=c("M","M","M","F","F")
> City=c("Kovai","Madurai","Erode","Chennai","Trichy")
> data2=data.frame(Gender,City)
> data2
Gender City
1 M Kovai
2 M Madurai
3 M Erode
4 F Chennai
5 F Trichy
> data3=cbind(data1,data2)
> data3
Name Age Height Weight Gender City
1 Kannan 24 145 45 M Kovai
2 Raju 55 170 58 M Madurai
3 Rajesh 58 160 69 M Erode
4 Manju 49 155 54 F Chennai
5 Kavi 15 150 45 F Trichy
> Name=c("Raji","Banu")
> Age=c(24,26)
> Height=c(170,156)
> Weight=c(50,45)
> data4=data.frame(Name,Age,Height,Weight)
> data4
Name Age Height Weight
1 Raji 24 170 50
2 Banu 26 156 45
> data5=rbind(data1,data4)
> data5
Name Age Height Weight
1 Kannan 24 145 45
2 Raju 55 170 58
3 Rajesh 58 160 69
4 Manju 49 155 54
5 Kavi 15 150 45
6 Raji 24 170 50
7 Banu 26 156 45
3.CREATING MATRICES & OPERATIONS WITH
MATRICES

CREATING MATRICES:
INPUT:

M<-matrix(c(3:14),nrow=4,byrow=TRUE)
M
rownames=c("row1","row2","row3","row4")
colnames=c("col1","col2","col3")
p<-matrix(c(3:14),nrow=4,byrow=TRUE,dimnames=list(rownames,colnames))
p
print(p[1,3])
print(p[,3])
print(p[1,])
matrix1<-matrix(c(3,9,-1,4,2,6),nrow=2)
print(matrix1)
matrix2<-matrix(c(5,2,0,9,3,4),nrow=2)
print(matrix2)

OUTPUT:

> M<-matrix(c(3:14),nrow=4,byrow=TRUE)
>M
[,1] [,2] [,3]
[1,] 3 4 5

[2,] 6 7 8
[3,] 9 10 11
[4,] 12 13 14
> rownames=c("row1","row2","row3","row4")
> colnames=c("col1","col2","col3")
> p<-matrix(c(3:14),nrow=4,byrow=TRUE,dimnames=list(rownames,colnames))
>p
col1 col2 col3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14
> print(p[1,3])
[1] 5
> print(p[,3])
row1 row2 row3 row4
5 8 11 14
> print(p[1,])
col1 col2 col3
3 4 5
> matrix1<-matrix(c(3,9,-1,4,2,6),nrow=2)
> print(matrix1)
[,1] [,2] [,3]
[1,] 3 -1 2
[2,] 9 4 6
> matrix2<-matrix(c(5,2,0,9,3,4),nrow=2)
> print(matrix2)
[,1] [,2] [,3]
[1,] 5 0 3
[2,] 2 9 4
>
OPERATIONS WITH MATRICES:
INPUT:

matrix1<-matrix(c(3,9,-1,4,2,6),nrow=2)
matrix2<-matrix(c(5,2,0,9,3,4),nrow=2)
#add the matrices
result<-matrix1+matrix2
cat("result of addition","\n")
print(result)
#subtract the matrices
result<-matrix1-matrix2
cat("result of subtraction","\n")
print(result)
matrix3<-matrix(c(2,4,6,3),nrow=2)
matrix4<-matrix(c(1,0,7,2),nrow=2)
result<-matrix3*matrix4
cat("result of multiplication","\n")
print(result)
result<-matrix3/matrix4
cat("result of division","\n")
print(result)
2*matrix3

OUTPUT:
> matrix1<-matrix(c(3,9,-1,4,2,6),nrow=2)
> matrix2<-matrix(c(5,2,0,9,3,4),nrow=2)
> #add the matrices
> result<-matrix1+matrix2
> cat("result of addition","\n")
result of addition
> print(result)
[,1] [,2] [,3]
[1,] 8 -1 5
[2,] 11 13 10
> #subtract the matrices
> result<-matrix1-matrix2
> cat("result of subtraction","\n")
result of subtraction
> print(result)
[,1] [,2] [,3]
[1,] -2 -1 -1
[2,] 7 -5 2
> matrix3<-matrix(c(2,4,6,3),nrow=2)
> matrix4<-matrix(c(1,0,7,2),nrow=2)
> result<-matrix3*matrix4
> cat("result of multiplication","\n")
result of multiplication
> print(result)
[,1] [,2]
[1,] 2 42
[2,] 0 6
> result<-matrix3/matrix4
> cat("result of division","\n")
result of division
> print(result)
[,1] [,2]
[1,] 2 0.8571429
[2,] Inf 1.5000000
> 2*matrix3
[,1] [,2]
[1,] 4 12
[2,] 8 6
4.TO CALCULATE MEAN,MEDIAN,STANDARD DEVIATION AND VARIANCE

INPUT:

rm(list=ls())
numeric_data=data.frame(x=c(45,72,89,56,45,77,88,82,41,36),y=c(85,96,32,14,52,78,86,42,12,6
3))
attach(numeric_data)
x
y
names(numeric_data)
x
mean(x)
mean(y)
median(x)
median(y)
sd(x)
sd(y)
var(x)
var(y)

OUTPUT:

> rm(list=ls())
> numeric_data=data.frame(x=c(45,72,89,56,45,77,88,82,41,36),y=c(85,96,32,14,52,78,86,42,12,63))
> attach(numeric_data)
>x
[1] 45 72 89 56 45 77 88 82 41 36
>y
[1] 85 96 32 14 52 78 86 42 12 63
> names(numeric_data)
[1] "x" "y"
>x
[1] 45 72 89 56 45 77 88 82 41 36
> mean(x)
[1] 63.1
> mean(y)
[1] 56
> median(x)
[1] 64
> median(y)
[1] 57.5
> sd(x)
[1] 20.67983
> sd(y)
[1] 30.48132
> var(x)
[1] 427.6556
> var(y)
[1] 929.1111
>
5.CONFIDENCE INTERVAL OF THE SAMPLE MEAN

INPUT:

#using normal
a<-5
s<-2
n<-20
error<-qnorm(0.975)*s/sqrt(n)
left<-a-error
left
right<-a+error
right
#using T
a<-5
s<-2
n<-20
error<-qt(0.975,df=n-1)*s/sqrt(n)
left<-a-error
right<-a+error
left
right

OUTPUT:
> #using normal
> a<-5
> s<-2
> n<-20
> error<-qnorm(0.975)*s/sqrt(n)
> left<-a-error
> left
[1] 4.123477
> right<-a+error
> right
[1] 5.876523
> #using T
> a<-5
> s<-2
> n<-20
> error<-qt(0.975,df=n-1)*s/sqrt(n)
> left<-a-error
> right<-a+error
> left
[1] 4.063971
> right
[1] 5.936029
>
6. CORRELATION BETWEEN AMOUNT OF FERTILIZERS AND YIELD OF
CROPS

INPUT:

fertilizer=c(15,18,20,24,30,35,40,50)
crops=c(85,93,95,105,120,130,150,160)
fertilizer
crops
cor(fertilizer,crops)
cor(fertilizer,crops,method="pearson")
cor(fertilizer,crops,method="kendall")
cor(fertilizer,crops,method="spearman")

OUTPUT:

> fertilizer=c(15,18,20,24,30,35,40,50)
> crops=c(85,93,95,105,120,130,150,160)
> fertilizer
[1] 15 18 20 24 30 35 40 50
> crops
[1] 85 93 95 105 120 130 150 160
> cor(fertilizer,crops)
[1] 0.9914885
> cor(fertilizer,crops,method="pearson")
[1] 0.9914885
> cor(fertilizer,crops,method="kendall")
[1] 1
> cor(fertilizer,crops,method="spearman")
[1] 1
>

You might also like