R Lab Programs (4) (Repaired) 1
R Lab Programs (4) (Repaired) 1
June – 2022
NAME :
REG. NO :
SEMESTER : IV
2 Simple Calculator
3 Multiplication Table
7 Hierarchal Clustering
8 Linear Regression
Aim
To Perform a R Program on adding two matrixes using array
Algorithm
8. result = m1 + m2
9. print("Result of addition")
10. print(result)
11. result = m1 - m2
12. print("Result of subtraction")
13. print(result)
14. result = m1 * m2
15. print("Result of multiplication")
16. print(result)
17. result = m1 / m2
18. print("Result of division:")
19. print(result)
Output Screen
[1] "Matrix-1:"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] "Matrix-2:"
[,1] [,2] [,3]
[1,] 0 2 0
[2,] 1 3 2
[1] "Result of addition"
[,1] [,2] [,3]
[1,] 1 5 5
[2,] 3 7 8
[1] "Result of subtraction"
[,1] [,2] [,3]
[1,] 1 1 5
[2,] 1 1 4
[1] "Result of multiplication"
[,1] [,2] [,3]
[1,] 0 6 0
[2,] 2 12 12
[1] "Result of division:"
[,1] [,2] [,3]
[1,] Inf 1.500000 Inf
[2,] 2 1.333333 3
Result
Thus, the program was successfully verified and Executed
2. Simple Calculator
Aim
To Perform a R Program on arithmetic operations as a simple
calculator.
Algorithm
Result
Thus, the program was successfully verified and Executed
3. Multiplication Table
Aim
To Perform a R Program on print the Multiplication table from the
given number.
Algorithm
Output:
Enter a number: 7
[1] "7 x 1 = 7"
[1] "7 x 2 = 14"
[1] "7 x 3 = 21"
[1] "7 x 4 = 28"
[1] "7 x 5 = 35"
[1] "7 x 6 = 42"
[1] "7 x 7 = 49"
[1] "7 x 8 = 56"
[1] "7 x 9 = 63"
[1] "7 x 10 = 70"
Result
Thus, the program was successfully verified and Executed
4. Find Sum, Mean and Product of Vector.
Aim
To Perform a R Program on Find Sum, Mean and Product of Vector.
Algorithm
Output:
[1] "Sum:"
[1] 60
[1] "Mean:"
[1] 20
[1] "Product:"
[1] 6000
Result
Thus, the program was successfully verified and Executed
5. To Convert Decimal into Binary using Recursive
Aim
To Perform a R Program on Decimal into Binary using Recursive
Algorithm
STEP 3: inside the function check if the given number is greater than 1, if yes
call function convert_to_binary() again with decnum/2 as the argument
if(n > 1) {
convert_to_binary(as.integer(n/2))
cat(n %% 2)
OUTPUT
> convert_to_binary(52)
110100
Result
Thus, the program was successfully verified and Executed
6. K-Means clustering technique.
Aim
To Perform a R Program on K-means clustering technique.
Algorithm
# Loading package
library(ClusterR)
library(cluster)
# Confusion Matrix
cm <- table(iris$Species, kmeans.re$cluster)
cm
## Visualizing clusters
y_kmeans <- kmeans.re$cluster
clusplot(iris_1[, c("Sepal.Length", "Sepal.Width")],
y_kmeans,
lines = 0,
shade = TRUE,
color = TRUE,
labels = 2,
plotchar = FALSE,
span = TRUE,
main = paste("Cluster iris"),
xlab = 'Sepal.Length',
ylab = 'Sepal.Width')
Output:
Model kmeans_re:
The 3 clusters are made which are of 50, 62, and 38 sizes
respectively. Within the cluster, the sum of squares is 88.4%.
Cluster identification:
Confusion Matrix:
Plot of clusters:
Result
Thus, the program was successfully verified and Executed
7. Hierarchal Clustering
Aim
To Perform a R Program on Hierarchal Clustering
Algorithm
Output :
Result
Thus, the program was successfully verified and Executed
8.Linear Regression.
Aim
To Perform a R Program on Linear Regression
Algorithm
OUTPUT
Call:
Coefficients:
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Aim
To Perform a R Program on adding two matrixes using array
Algorithm
$ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
hist(Temperature)
hist(Temperature,
xlim=c(50,100),
col="darkmagenta",
freq=FALSE
)
OUTPUT
Result
Thus, the program was successfully verified and Executed
10.
To Visualize the data using Box plot.
Aim
To Perform a R Program on adding two matrixes using array
Algorithm
> str(airquality)
'data.frame': 153 obs. of 6 variables:
$ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
$ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
$ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
$ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
$ Month : int 5 5 5 5 5 5 5 5 5 5 ...
$ Day : int 1 2 3 4 5 6 7 8 9 10 ...
boxplot(airquality$Ozone)
boxplot(airquality$Ozone,
main = "Mean ozone in parts per billion at Roosevelt Island",
xlab = "Parts Per Billion",
ylab = "Ozone",
col = "orange",
border = "brown",
horizontal = TRUE,
notch = TRUE
)
OUTPUT
Result
Thus, the program was successfully verified and Executed
11.
To write a R program to Visualize the Scatter plot.
Aim
To Perform a R Program on adding two matrixes using array
Algorithm
x <- mtcars$wt
y <- mtcars$mpg
# Plot with main and axis titles
# Change point shape (pch = 19) and remove frame.
plot(x, y, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE)
# Add regression line
plot(x, y, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE)
abline(lm(y ~ x, data = mtcars), col = "blue")
Result
Thus, the program was successfully verified and Executed