You are on page 1of 31

1.

Creating Vectors, matrices, factors and plotting


Aim:
To create vectors, matrices, factors and plotting graphs.
Algorithm:
Step 1: Click Start menu.
Step 2: Click R- Programme.
Step 3: Creating vectors, matrices, factors and plotting graphs.
Step 4: The vector is numeric, complex, logical, character are enter
the program.
Step 5: The matrices enter the 2X3 matrices
+n row=3
+n col=2
Step 6: The factors are defined
direction > direction <- c(“North”, “East”, ”South”)
>factor(direction, Levels=c(“North”, “East”, ”South”,
”West”))[1].
Levels: North East South West.
Step 7: To create a plotting graphs.
x <- seq(-pi, pi, 0, 1)
plot(x, sin(x))
Step 8: Output will be displayed.
Step 9: Stop the process.
Program:
 Vectors
vector(“numeric”, 4)
#[1] 0 0 0 0
vector(“complex”, 4)
#[1] 0+0i 0+0i 0+0i 0+0i
vector(“logical”, 4)
#[1] FALSE FALSE FALSE FALSE
vector(“character”, 4)
##[1] “ “ “ “ “ “ “ “

 Matrices
>B = matrix(
+c(2, 4, 3, 1, 5, 7)
+n row=3
+n col=2)
>B
[,1] [,2]
[,1] 2 1
[,2] 4 5
[,3] 3 7

 Factors
>directions <- c(“North”, “East”, ”South”, ”South”)
>factor(directions)
[1] North East South South
Levels : East North South
>factor(direction,Levels=c(“North”, “East”, ”South”, ”West”))[1]
North East South South
Levels : North East West South

 Plotting graphs:
x <- seq(-pi, pi, 0, 1)
plot(x, sin(x))

Result:
Thus the output has been verified and executed successfully.
2. Import data, copy data from excel to R
Aim:
To write R – Programme to import data, copy data from excel to R.
Algorithm:
Step 1: Click start menu.
Step 2: Click R-Programme.
Step 3: Create import data and copy data.
Step 4: Go to the excel and enter the datas.
S.No Fruits Price
1 Apple 80
2 Orange 65
3 Pome 100
4 Pine 79
5 Grapes 59
Step 5: chosen.txt <- read.table(file.choose(), header=7)
Step 6: chosen.txt
Step 7: output will be displayed.
Step 8: Stop the process.

Program:
Import and copy data.
choosen.txt <- read.table(file.choose(), header=7)
chosen.txt
Fruits Price
1 Apple , 80
2 Orange, 65
3 Pome , 100
4 Pine , 79
5 Grapes, 59
>write.table(chosen.txt, “geoff.txt”)
>file.exists(“geoff.txt”)
[1] TRUE

Result:
Thus the output has been verified and executed successfully.

Output:

“FRUIT PRICE”
“1” “APPLE , 80”
“2” “ORANGE , 65”
“3” “POME , 100”
“4” “PINE , 79”
“5” “GRAPES , 59”

3. Variables and data in R


Aim:
To write a R-programme to work with variables and data In R.
Algorithm:
Step 1: Click start menu.
Step 2: Click R programme.
Step 3: To create a variable data in R-program.
Step 4: Enter the values of numeric, complex, character.
Step 5: Output will be displayed.
Step 6: Stop the process
Program:
>a=49
>class(a)
[1]”numeric”

>a=5
>calss(a)
[1]”numeric”

>z=5
>class(a)
[1]”complier”
>a=”hello”
>class(a) or type of (a)
>class(a)
[1]”character”
>a
[1]”hello”
Result:
Thus the output has been verified and executed successfully.
4.Logic statement

Aim:
To write a R-program use logic statement.
Algorithm:
Step 1: Click start menu.
Step 2: Click R-programme.
Step 3: Enter the values of logic statement.
Step 4: The output may be true, False.
Step 5: Stop the process.
Program:
a=3
b=6
c=a<b
c
[1] TRUE
C=a<b
C
[2] FALSE

Result:
Thus the output has been verified and Executed successfully.

Age count of 10 students

7-

6-

5-
4-

3-

2-

1-

0-
16 17 18 19
Age
5. Bar Charts and Pie Charts
Aim:
To write a R-Programme to draw Bar charts and pie charts.
Algorithm:
Step 1: Click the start menu.
Step 2: Goto All Program. Select R- Programme.
Step 3: To create Bar chart enter the data named age
>age <- c(17,18,18,17,18,19,18,16,18,18)
Step 4: Create a table age
>table(age)
Column1
Step
China
35%
5:
Bhutan Input
12%
the
Nepal
data
11%
foe
Srilanka
9%
India
33%

India Srilanka Nepal Bhutan China

barplot and designing barchart.


>barplot(table(age), main=”Age count of 10 students”,
Xlab=”Age”, ylab=”count”, border=”red”, col=”blue”,
density=10).
Step 6: To create pie chart enter the values for it.
>value <- c(906,254,289,339,938)
>countries <- c(“India”, “Srilanka”, “Nepal”, “Bhutan”,
“China”).
Step 7: Input the pie values are
>pie(values, labels=countries).
Step 8: Output will ba displayed.
Step 9: save the output.
Step 10: Stop the Process.
Program:
>age <- c(17,18,18,17,18,19,18,16,18,18)
>table(age)
>age
16 17 18 19
1 2 6 1
>barplot(table(age), main=”Age count of 10 students”,
xlab=”Age”, ylab=”count”, border=”red”, col=”blue”,
density=10)
>value <- c(906,254,289,339,938)
>countries <- c(“India”, “Srilanka”, “Nepal”, “Bhutan”, “China”).
>pie(values, labels=countries).

Result:
Thus the output has been verified and executed successfully.

6. Histogram in R
Aim:
To create a R Programme to draw Histogram in R.
Algorithm:
Step 1: Click the start menu.
Step 2: Select R Programme.
Step 3: We will need the MASS and rpart libraries.
Step 4: Load up the data.
library(MASS)
library(rpart)
Step 5: To display the headings of the dataset.
Head(birthwt)
Step 6: By the above head()function we get the dataset.
Step 7: To display the histogram.
List(birthwt$bwt)
Step 8: Output will be displayed.
Step 9: Save the output.
Step 10: Stop the programme.

Program:
>library(MASS)
>library(rpart)
>head(birthwt)
low age lwt race smoke ptl ht ui ftv bwt
85 0 19 182 2 0 0 0 1 0 2523
86 0 33 155 3 0 0 0 0 3 2551
87 0 20 105 1 1 0 0 0 1 2557
88 0 21 108 1 1 0 0 1 2 2594
89 0 18 107 1 1 0 0 1 0 2600
91 0 21 104 3 0 0 0 0 0 2622
>head(birthwt$bwt)

Result:
Thus the output has been verified and executed successfully.
7. Mean, Standard deviation, Frequencies and t-test
Aim:
To create a R-Programme to calculate mean, standard deviation,
frequency and t-test.
Algorithm:
Step 1: Click start menu.
Step 2: Click R-Programme.
Step 3: calculate mean, standard deviation, frequency and t-test.
Step 4: Find the mean
> mean.result = mean(x)
>print(mean.result)
[1] 2.8
Step 5: Calculate the comparison of t-distribution.
main = (comparison of t-distribution, col =“block”)
lines (x,y-2,col=”red”)
lines(x,y-3,col=”orange”)
lines(x,y-4,col=”green”)
lines(x,y-5,col=”blue”)
Step 6: Output will be displayed.
Step 7: Stop the process.
Program:
>print(sd.result)
[1] 1.576138
>x <- c(1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6)
>mean.result=mean(x)
>print (mean.result)
[1] 28
x <- (1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6)
>median.result=median(x)
>print(median.result)
[1] 2.5
X <- seq(-4,4, length=100)
y-1 <- dt(x ,df=4)
y-2 <- dt(x, df=6)
y-3 <- dt(x, df=8)
y-4 <- dt(x, df=10)
y-5 <- dt(x, df=12)
plot(x, y-1, type=”1”, lwd=2, xlab=”t-value”, ylab=”density”,
main=”comparison of, t distributions”, col=”black”)
lines(x, y-2, col=”red”)
lines(x, y-3, col=”orange”)
lines(x, y-4, col=”green”)
lines(x, y-5, col=”blue”)
legend(“top right”, c(“df=4”, “df=6”, “df=8”, “df=10”, “df=12”)
col=c(“black”, “red”, “orange”, “green”, “blue”)
title =”t-distributions”, lty=1)

Result:
Thus the output has been verified and executed successfully.
8. Variance using ANOVA
Aim:
To create a R Programme to test significant difference between the
variance using ANOVA.
Algorithm:
Step 1: Click the start menu.
Step 2: Select the R Programme.
Step 3: We will need the multcomp library.
>library(multcomp)
Step 4: Attach a dataset named cholesterol using the attach()
function.
>attach(cholesterol)
Step 5: Create a table naming txt.
>table(txt)
Step 6: Group means using aggregate() function.
>aggregate(response, by=list(txt), FUN=mean)
Step 7: Group standard deviation using aggregate() function.
>aggregate(response, by=list(txt), FUN=sd)
Step 8: Declare fit as.
>fit <- aov(response~txt)
Step 9: To find the summary of fit.
>summary(fit)
Step 10: We need gplots library.
>library(gplots)
Step 11: After the detach the dataset.
>detach(cholesterol)

Program:
>library(multcomp)
>attach(cholesterol)
>table(txt)
>txt.
1time 2times 4times drugD drugE
10 10 10 10 10
>aggregate(response, by=list(txt), FUN=mean)
Group 1 x
1. 1time 5.78
2. 2times 9.22
3. 3times 12.37
4. drugD 15.36
5. drugE 20.95
>library(gplots)
>plotmeans(response~txt, xlab=”Treatment”, ylab=”Response”,
main=”Mean plot/n with 95% C.I”)
>detach(cholesterol)
Result:
Thus the output has been verified and executed successfully.

9.Chi Square test


Aim:
To create a R-programme to perform chi-square test.
Algorithm:
Step 1: Click start menu.
Step 2: Click R-Programme.
Step 3: Calculate the chi-square test.
Step 4: load the library.
Step 5: Create a data frame from the main dataset.
Step 6: Create a table with needed variables.
Step 7: Perform the chi-square test.
Step 8: Output will be displayed.
Step 9: Stop the process.

Program:
#load the library.
library(“MASS”)
#create s data frame from the main dataset.
car.data <- data.frame(cars93$Airbags, cars93$type)
#create a table with the needed variables.
car.data = table(cars93$Airbags, cars93$type)
print(car.data)
#perform the chi-square test.
Print(chisq.test(car.data))

Result:
Thus the output has been verified and executed successfully.

10.Correlation coefficient between two variables.


Aim:
To create a R programme to calculate correlation coefficient
between two variables.
Algorithm:
Step 1: Click start menu.
Step 2: Click R Programme.
Step 3: To calculate correlation coefficient between two variables.
Step 4: Defined x and y variables.
x <- c(44.4, 45.4, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
>y <- c(2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
Step 5: To define correlation method.
>cor(x,y,method=c(“Pearson”, “Kendall”, “Spearman”))
Step 6: Calculate the method “Pearson”
>res <- core.test(x,y,method=”Pearson”)
Step 7: Output will be displayed.
Step 8: Stop the process.

Program:
x <- c(44.4, 45.4, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
>y <- c(2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
>cor(x,y,method=c(“Pearson”, “Kendall”, “Spearman”))
[1] 0.5711816
>cor(x, y, method=”Pearson”)
[1] 0.57811816
>cor(x, y, use=”complete.obs”)
[1] 0.57811816
>res <- core.test(x,y,method=”Pearson”)
>yes

Result:
Thus the output has been verified and executed successfully.

11.Regression equation for the given set of values


Aim:
To create a R-program to fit regression for the given set of values.
Algorithm:
Step 1: click the start menu.
Step 2: select R program.
Step 3: Importing the dataset for regression
>dataset=read.csv (‘salary.csv’)
Step 4: Then split the dataset into training set and test set also he
read packages and library.
>install.packages (‘ca tools’)
>library (‘ca tools’)
>split =sample.split (dataset$salary ,split ratio=0.7)
>trainglist =subset (dataset,split==TRUE)
>testset =subset (dataset,split==FALSE)
Step 5: After that fitting regression to the trainglist
data=(triangle set)
>coef(lm.r)
Step 6: Preceding the test set results.
>y pred =predict (lm,l,new data=test set)
Step 7: We need packages and libraries.
>install packages (“ggplot2”)
>library(ggplot 2)
Step 8: visualizing the training set results.

Program:
>fit<-lm (weight-height, data =women)
>summary (fit)
Call:
lm (formulas=weight-height, data=women)
Residuals:
Min 1Q Median 3Q Max
-1.733 -1.133 -0.383 0.742 3.117
Coefficient:
Estimate std.error t value pr(>111)
(Intereapt) -87.5167 5.9369 -14.7 1.7e-09***
Height 3.4500 0.0911 37.9 1.1e-14***
Signif.codes: 0’***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘’1
Residual standard error: 1.53 on 13 degreesof freedom.
Multiple R-squared: 0.991, Adjusted R-squared :0.99
F-statistics: 1.432 +03 on 1 and 13 of,p-value :1.09e=14.
>women$weight.
[1] 115 117 120 123 126 132 135 139 142 146 150 154 159 164
>fitted (fit)
1 2 3 4 5 6 7 8 10
112.58 116.03 119.48 122.93 126.38 129.83 133.28 136.73 143.63
11 12 13 14 15
147.08 150.53 153.98 157.43 160.88
>residuals (fit)
1 2 3 4 5 6 7 8 9 12 13 14
2.42 0.97 0.52 0.07 -0.38 -0.83 -1.28 -1.73 -1.18 -0.53 0.02 1.57

15
3.12
>plot (women$height, women$weight),
Xlab =”Height (in inches)”,
Ylab =”weight (in pounds)”)
>abline (fit)

Result:
Thus the output has been verified and executed successfully.

You might also like