Professional Documents
Culture Documents
PRACTICAL NO.1
AIM: Program to execute the basic commands, array, list and frames in R programming
language.
Q1. Take any two variables a and b. Write a R-code to perform arithmetic operations on them.
R-CODE:
> a = 10
>b=5
> a+b
15
> a-b
> a*b
50
> a/b
> a^3
1000
> exp(a)
22026.47
> a = 25 ; sqrt(a)
> b = - 9 ; abs(b)
Q.4 Create two vectors p and q. Write a R code to perform Logical operations.
p = c(4, 2.2, 8, 6) and q = c(5, 10, 8, 6.6)
John 18 FALSE
Alex 19 TRUE
Jay 20 TRUE
Monisha 18 TRUE
Sam 21 TRUE
Rhea 19 TRUE
If we want to combine two data frames vertically, the column name of the two
data frames must be the same.
Monisha 18 TRUE
Sam 21 TRUE
Rhea 19 TRUE
City Country
Mumbai India
Bengluru India
Toronto Canada
PRACTICAL NO.2
Q.1. Create two matrices A and B. Write a R-code to perform mathematical operations.
3 4 5
6 7 8
9 10 11
3 6 9
4 7 10
5 8 11
R-code:
> A = matrix ( c(3:11),nrow=3,byrow="true" )
>A
[,1] [,2] [,3]
[1,] 3 4 5
[2,] 6 7 8
[3,] 9 10 11
> A+B
[,1] [,2] [,3]
[1,] 6 10 14
[2,] 10 14 18
[3,] 14 18 22
> A-B
[,1] [,2] [,3]
[1,] 0 -2 -4
[2,] 2 0 -2
[3,] 4 2 0
> A%*%B
> A/B
[,1] [,2] [,3]
[1,] 1.0 0.6666667 0.5555556
[2,] 1.5 1.0000000 0.8000000
[3,] 1.8 1.2500000 1.0000000
>t(A)
[,1] [,2] [,3]
[1,] 3 6 9
[2,] 4 7 10
[3,] 5 8 11
> t(B)
[,1] [,2] [,3]
[1,] 3 4 5
[2,] 6 7 8
[3,] 9 10 11
Q.2. Write a R-Code to verify the following expressions for given matrices:
i) 2 2
( A+ B) = A + AB+BA +B
2
PRACTICAL NO. 3
AIM: Program to execute the Statistical functions on the ungrouped / Raw data.
R-CODE:
> x = c(20, 30, 10 ,10, 25, 12)
> print(result1)
[1] 17.83333
> print(result2)
[1] 16
{m = unique(v)
m[which.max(tabulate(match(x, m)))]
> print(result)
[1] 10
Q.1. write a R-Code to find the Range, IQR, Coefficient of range of Rainfall
measured during 9 days in millimeters: 29,19, 26,12, 24, 21, 36, 35, 33
R-Code:
>r
[1] 24
> IQR(Rainfall)
[1] 12
> cr
[1] 0.5
R-Code:
>Q
> QD
75%
13.125
> CQ
75%
0.443038
R-Code:
> sd(Rainfall)
[1] 8.006941
> var(Rainfall)
[1] 64.11111
PRACTICAL NO. 4
AIM: To import the data from Excel / .CSV file and to perform the measures of central
tendency.
Q1. Import the given data from the .csv file. Write a R-code to calculate measures
of central tendency of marks for each subject.
R-Code:
>dir()
> d = read.csv("Data.csv")
>d
Name Marks..English. Marks..Maths.
1 Jay 30.0 40.0
2 Alex 25.5 50.0
3 Caroline 35.0 45.5
4 Esmita 45.5 25.5
5 Nick 45.0 33.0
6 Nimit 42.5 35.0
{m = unique(v)
m[which.max(tabulate(match(d$Marks..English, m)))]
> print(result)
Q2.
1 A 6000 2012-01-01 IT
2 B 5000 2013-09-23
Operations
3 C 6500 2014-11-15 IT
4 D 7300 2014-05-11 HR
6 F 5500 2013-05-21 IT
Q2. Get the persons having maximum salary and minimum salary.
R-code:
>dir()
> data = read.csv("input.csv")
> print(data)
1 1 A 6000 2012-01-01 IT
3 3 C 6500 2014-11-15 IT
4 4 D 7300 2014-05-11 HR
6 6 F 5500 2013-05-21 IT
Q1.
print(ncol(data))
print(nrow(data))
[1] 5
[1] 8
Q2.
> print(Max)
> print(Min)
Q3.
> print(result)
3 3 C 6500 2014-11-15 IT
4 4 D 7300 2014-05-11 HR
Q4.
> print(Mean)
[1] 6587.5
> print(Median)
[1] 6500
> print(Mode)
[1] "numeric"
PRACTICAL NO. 5
AIM: To import the data from Excel / .CSV file and to perform the measures of
dispersion.
Q1. Import the given data from the .csv file. Write a R-code to calculate measures of
dispersion of marks for each subject.
R-Code:
>dir()
> d = read.csv("Data.csv")
>d
Name Marks..English. Marks..Maths.
1 Jay 30.0 40.0
2 Alex 25.5 50.0
3 Caroline 35.0 45.5
4 Esmita 45.5 25.5
5 Nick 45.0 33.0
6 Nimit 42.5 35.0
> SD = sd(d$Marks..English)
> SD
8.383019
> SD = sd(d$Marks..Maths)
> SD
8.880691
> IQR(d$Marks..Maths)
10.625
[1] -66
There exists a negative relationship between two subjects.
i)
> SD = sd(d$salary)
> SD
1088.167
> IQR(d$salary)
1575
ii)
>cov(d$salary, d$exp_salary)
>cov
1443750
There exists a positive correlation
iii)
plot(col1 , col2)
There exists a positive correlation among current salary and
expected salary.
PRACTICAL NO. 6
AIM: To import the data from .CSV file and to calculate Skewness.
Q1. Import the given data from the .csv file. Write an R-code to
calculate the Skewness and Kurtosis of scores obtained by 9
candidates. Also plot the skewness.
Candidates Alex John Steve Nimita James Caroline Vansh David Sumati
R-Code:
> dir()
"Pract 6 Data.csv"
>d
Candidates Score
1 Alex 10.0
2 John 45.5
3 Steve 80.0
4 Nimit 65.0
5 James 57.5
6 Caroline 39.5
7 Vansh 35.0
8 David 25.0
9 Sumati 15.0
> SD = sd(d$Score )
R.K.Talreja College > plot(y,xlab='Probability',
ylab='Quantiles', type='h',col='Purple')
Page No
SYBSc (IT) - Sem IV Computer Oriented Statistical Techniques
> Sk
[1] 0.2438718
To Plot Skewness:
> x = c(10, 45.5, 80, 65, 57.5, 39.5, 35, 25, 15)
> plot(x, type="o", main = "Skewness")
> kurtosis(d$Score)
1.992505
Interpretation:
Q2. Import the given data from the .csv file. Write an R-code to
calculate the Skewness and kurtosis of scores obtained by 7
candidates. Also plot the skewness.
Score 10 20 30 50 40 30 20
> kurtosis(d$Score)
2.123961
Practical No- 07
AIM: Study of Hypothetical testing (Upper tailed & Lower tailed tests) in R.
qnorm: To get percentiles from a normal distribution with mean µ and standard
deviation σ. It is inverse of pnorm i.e. CDF (Cumulative Distribution Function).
Q.1. Suppose the food label on a cookie bag states that there is at most 2 grams of
saturated fat in a single cookie. In a sample of 35 cookies, it is found that the mean
amount of saturated fat per cookie is 2.1 grams. Assume that the population standard
deviation is 0.25 grams. At a 0.05 significance level, can we reject the claim on the food
label?
Hypothesis:
The null hypothesis is H0 : μ 2
The Alternative hypothesis is H1 : μ = 2
R-Code:
xbar = 2.1 # sample mean
mu = 2 # hypothesized value
sigma = 0.25 # population standard deviation
n = 35 # sample size
z
[1] 2.3664
Result:
The z-value 2.3664 is greater than the critical value of 1.6449
Hence at 0.05 significance level, we reject (null hypothesis) the claim that there is at
most 2 grams of saturated fat in a cookie.
So we accept H1
Q.2. Suppose the food label on a cookie bag states that there is at most 2 grams of
saturated fat in a single cookie. In a sample of 35 cookies, it is found that the mean
amount of saturated fat per cookie is 2.1 grams. Assume that the sample standard
deviation is 0.3 gram. At a 0.05 significance level, can we reject the claim on the food
label?
Hypothesis:
The null hypothesis is H0 : μ 2
The alternative hypothesis is H1 : μ = 2
R-Code:
Result:
The t-value 1.9720 is greater than the critical value of 1.6991.
Hence at 0.05 significance level, we can reject (null hypothesis) the claim that there is
at most 2 grams of saturated fat in a cookie.
So we accept H1
Practical No- 8
Functions Description
Q.1 A fair coin is tossed 10 times. What is the probability of getting heads exactly 8
times? Generate pdf function for Binomial distribution in R.
R-Code:
> x = 1 : 10
> n = 8; p = 0.5
> y = dbinom(x, n, p)
> data.frame(y)
1 0.03125000
2 0.10937500
3 0.21875000
4 0.27343750
5 0.21875000
6 0.10937500
7 0.03125000
8 0.00390625
9 0.00000000
10 0.00000000
Q.2 A fair coin is tossed 10 times. What is the probability of getting heads at most 8
times (8 or less than)? Generate cdf function for Binomial distribution in R.
R-code:
> x = 1 : 10
> n = 8; p = 0.5
> y = pbinom(x, n, p)
> data.frame(y)
1 0.03515625
R.K.Talreja College > plot(y,xlab='Probability',
ylab='Quantiles', type='h',col='Purple')
Page No
SYBSc (IT) - Sem IV Computer Oriented Statistical Techniques
2 0.14453125
3 0.36328125
4 0.63671875
5 0.85546875
6 0.96484375
7 0.99609375
8 1.00000000
9 1.00000000
10 1.00000000
Q3. Write a R-code to calculate the 19th quantile of a Binomial distribution with 30
trials with the prob of success to 0.5.
R-code:
>y
[1] 13
Q.4 Write a R-code to calculate the 6 random samples from 10 samples with
probability 0.5
R-code:
> x = rbinom(N, n, p)
>x
[1] 4 3 6 4 6 4
> plot(x,xlab='Samples',ylab='Probability',type='h',col='Purple')
Functions Description
qnorm (p1, mean, sd) Quantile function of the Normal distribution. Quantile
function gives value of x such that P(X <= x)=p1.
Hence, the qnorm function is the inverse of the pnorm
function.)
· x is a vector of numbers.
· p is a vector of probabilities.
· N is the number of samples (sample size).
· Here, mean is the mean value of the sample data. Also, its default value is zero.
· sd is the standard deviation. Its default value is 1.
R Code:
x = -4:4
mean = 1
sd = 3
plot(x, y)
R-Code:
x = - 4:4
mean = 1
sd = 1
plot(x,y)
R-Code:
N=6
mean = 40
sd = 2
R.K.Talreja College > plot(y,xlab='Probability',
ylab='Quantiles', type='h',col='Purple')
Page No
SYBSc (IT) - Sem IV Computer Oriented Statistical Techniques
Output: