R Tools LAB
R Tools LAB
NO:1
DATE:- 23-01-24
Find the sum product and mean of vector in r tools
ALGORITHM
STEP 1: Use the built-in functions
STEP 2: Call sum() with vector and na.rm = TRUE as argument
STEP 3: Call mean() with vector and na.rm = TRUE as argument
STEP 4: Call prod() with vector and na.rm = TRUE as argument
STEP 5: Print the result of each function
R Source Code
OUTPUT
[1] "Sum is:"
[1] 90
[1] "Mean is:"
[1] 30
[1] "Product is:"
[1] 24000
1
EX.NO:2
DATE:- 24-01-24
R program to sample from a population
A vector of ages of people in a community and you want to randomly select 5 ages for a survey.
CODE:-
Output:
[1] 25 40 55 45 70
2
# Create a list of favorite colors
favorite_colors <- list("Red", "Blue", "Green", "Yellow", "Purple", "Orange")
# Convert the list into a vector
colors_vector <- unlist(favorite_colors)
# Perform simple random sampling of 2 colors
sampled_colors <- sample(colors_vector, size = 2, replace = FALSE)
# Print the sampled colors
print(sampled_colors)
Output:
[1] "Red" "Green"
# Create a sample data frame
employees <- data.frame(
Name = c("John", "Jane", "Jim", "Jill", "Joe", "Janet"),
Age = c(30, 35, 25, 40, 45, 28),
Salary = c(50000, 60000, 45000, 70000, 80000, 55000))
# Perform simple random sampling of 2 employees
sampled_employees <- employees[sample(nrow(employees), size = 2,
replace = FALSE), ]
# Print the sampled employees
print(sampled_employees)
Output:
Name Age Salary
1 John 30 50000
6 Janet 28 55000
3
EX.NO:3
DATE:- 31-01-24
CODE:-
# Creating a vector
x <- c(7, 4, 3, 9, 1.2, -4, -5, -8, 6, NA)
# Calling sort() function
sort(x)
Output:
[1] -8.0 -5.0 -4.0 1.2 3.0 4.0 6.0 7.0 9.0
# Creating a vector
x <- c(7, 4, 3, 9, 1.2, -4, -5, -8, 6, NA)
# Calling sort() function
# to print in decreasing order
sort(x, decreasing = TRUE)
# Calling sort() function
# to print NA at the end
sort(x, na.last = TRUE)
Output:
[1] 9.0 7.0 6.0 4.0 3.0 1.2 -4.0 -5.0 -8.0
[1] -8.0 -5.0 -4.0 1.2 3.0 4.0 6.0 7.0 9.0 NA
4
EX.NO:5
DATE:- 06-02-24
CODE:-
x <- c(1, 2, 3)
y <- c("G1", "G2", "G3")
5
EX.NO:6
DATE:- 06-02-24
CODE:-
#Create sequence elements
seq(1:10)
# R Program to illustrate
# the use of seq() Function
vec1
# R Program to illustrate
# the use of seq() Function
[1] 1 2 3 4 5 6 7 8 9 10
[1] 1 3 5 7 9
[1] 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
6
EX.NO:7
DATE:- 06-02-24
data_gfg=data.frame(x=c(1,3,5,3,4),
y=c(1,5,1,4,5))
table_gfg=table(data_gfg$x,data_gfg$y)
print("table in use: ")
table_gfg
new_data_gfg=as.data.frame.matrix(table_gfg)
print("converted to dataframe: ")
new_data_gfg
7
EX.NO:8
DATE:- 07-02-24
7. Calculate student mark list and output in its data frame.
AIM:- To calculate student mark list and output in its data frame.
CODE:-
# R program to assign
#grades to a student
# using nested if-else
#Store marks of all the
#subjects in an array
marks <- c(95, 88, 98, 93, 92, 96)
#Max marks will be
#100 * number of subjects
max_marks <- length(marks) * 100
# Initialize student's
#total marks to 0
total <- 0
# Initialize student's
# grade marks to F
grade<- 'F'
#find the sum of the array
total <- sum(marks)
# Calculate the percentage.
percentage <- (total/ max_marks) *100
# Nested if else
if (percentage >= 90) {
grade <- 'A'
} else {
if (percentage >= 80 && percentage <= 89) {
grade <- 'B'
} else {
if (percentage >= 60 && percentage <= 79) {
grade <- 'C'
} else {
if (percentage >= 33 && percentage <= 59) {
grade <- 'D'
} else {
grade <- 'F' } } }}
print(grade)
Output:
[1] "A"
8
EX.NO:9
DATE:- 07-02-24
CODE:-
Find_Prime_No <- function(n1) {
if (n1 == 2) {
return(TRUE)
}
if (n1 <= 1) {
return(FALSE)
}
for (i in 2:(n1-1)) {
if (n1 %% i == 0) {
return(FALSE)
}
}
return(TRUE)
}
numb_1 <- 13
if (Find_Prime_No(numb_1)) {
# Using paste function to include the number in the output
print(paste(numb_1, "is a prime number"))
} else {
print("It is not a prime number")
}
}
Output:
9
EX.NO:10
DATE:- 07-02-24
CODE:-
# Function to check for a leap year
is_leap_year <- function(year) {
if ((year %% 4 == 0 && year %% 100 != 0) || year %% 400 == 0) {
return(TRUE)
} else {
return(FALSE)
}
}
# Input year
input_year <- 2024
Output:
[1] "2024 is a leap year."
10
EX.NO:11
DATE:- 07-02-24
CODE:-
number <- 15
if (number%%2 == 0) {
print("Number is even")
} else {
print("Number is odd")
}
OUTPUT:-
[1] "Number is odd"
EX.NO:12
11
DATE:- 13-02-24
CODE:-
sum<-function(n){
if (n<=1){
return(n)
}else{
return(n+sum(n-1))
}
}
sum(7)
OUTPUT :-
[1] 28
12
EX.NO:13
DATE:- 13-02-24
CODE:-
OUTPUT:-
110100
13
EX.NO:14
DATE:- 13-02-24
CODE:-
OUTPUT:-
[1] "The factorial of 5 is 120"
14
EX.NO:15
DATE:- 20-02-24
14. R Program to make a simple calculator.(RS)
AIM:- R Program to make a simple calculator.(RS)
CODE:-
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]: "))
n1 = as.integer(readline(prompt="Enter first number: "))
n2 = as.integer(readline(prompt="Enter second number: "))
operator <- switch(choice,"+","-","*","/")
result <- switch(choice, add(n1, n2), subtract(n1, n2), multiply(n1, n2), divide(n1,
n2))
print(paste(n1, operator, n2, "=", result))
OUTPUT:-
[1] "Select operation."
[1] "1.Add"
[1] "2.Subtract"
[1] "3.Multiply"
15
[1] "4.Divide"
Enter choice[1/2/3/4]: 4
Enter first number: 40
Enter second number: 2
[1] "40/ 2 = 20"
16
EX.NO:16
DATE:- 20-02-24
CODE:-
In the dropdown menu, select the From Text (readr)... option. This will open a file explorer.
Click on the Browse button and navigate to your CSV file in the file explorer, select it, and
click Open.
R Studio will open a data import window. Here, you can tweak how R Studio reads your CSV
file. You can set things like whether the first row contains the column names or the maximum
number of rows to read in.
Once you’ve adjusted the settings to fit your needs, click on the Import button.
Your CSV data will be imported into R Studio as a data frame.
OR
rCopy code
setwd("/path/to/your/directory")
17
EX:2
OUTPUT:-
Output:
EX.NO:17
18
DATE:- 20-02-24
CODE:-
product Price
computer 800
monitor 450
keyboard 100
printer 150
CODE:-
print(df)
write.csv(df, "C:\\Users\\Ron\\Desktop\\Test\\my_products.csv",
row.names=FALSE)
OUTPUT :-
19
product price
1 computer 800
2 monitor 450
3 keyboard 100
4 printer 150
EX.NO:18
DATE:- 20-02-24
OUTPUT:-
20
EX.NO:19
DATE:- 21-02-24
CODE:-
print(result)
OUTPUT:-
EX.NO:20
DATE:- 21-02-24
19. Create a Table from the existing data set in R and draw the chart.
AIM:- Create a Table from the existing data set in R and draw the chart
CODE:-
#make this example reproducible
set.seed(1)
#define data
df <- data.frame(team=rep(c('A', 'B', 'C', 'D'), each=4),
pos=rep(c('G', 'F'), times=8),
points=round(runif(16, 4, 20),0))
22
#view head of data
head(df)
barplot(table(mtcars$carb), col="green")
OUTPUT:-
ABCD
F2222
G2222
23
EX.NO:21
DATE:- 23-02-24
20. Apply K-Means Algorithm for IRIS data set and output it in graph.
AIM:- Apply K-Means Algorithm for IRIS data set and output it in graph.
CODE:-
# Installing Packages
#install.packages("ClusterR")
#install.packages("cluster")
# 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,
25
plotchar = FALSE,
span = TRUE,
main = paste("Cluster iris"),
xlab = 'Sepal.Length',
ylab = 'Sepal.Width')
OUTPUT:-
K-means clustering with 3 clusters of sizes 50, 62, 38
Cluster means:
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.006000 3.428000 1.462000 0.246000
2 5.901613 2.748387 4.393548 1.433871
3 6.850000 3.073684 5.742105 2.071053
Clustering vector:
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1
[38] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2
[75] 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3 3 3
3
[112] 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3 3 3 2
3
[149] 3 2
Available components:
26
> kmeans.re$cluster
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1
[38] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2
[75] 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3 3 3
3
[112] 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3 3 3 2
3
[149] 3 2
> cm <- table(iris$Species, kmeans.re$cluster)
> cm
1 2 3
setosa 50 0 0
versicolor 0 48 2
virginica 0 14 36
> kmeans.re$centers
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.006000 3.428000 1.462000 0.246000
2 5.901613 2.748387 4.393548 1.433871
3 6.850000 3.073684 5.742105 2.071053
> kmeans.re$centers[, c("Sepal.Length", "Sepal.Width")]
Sepal.Length Sepal.Width
1 5.006000 3.428000
2 5.901613 2.748387
3 6.850000 3.073684
27
EX.NO:22
DATE:- 27-02-24
21. Get some input from mtcar data set and perform analysis.
AIM:- Get some input from mtcar data set and perform analysis.
CODE:-
28
head(mtcars)
OUTPUT:-
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
29
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
> summary(mtcars)
mpg cyl disp hp
Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
Median :19.20 Median :6.000 Median :196.3 Median :123.0
Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
drat wt qsec vs
Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
Median :3.695 Median :3.325 Median :17.71 Median :0.0000
Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
am gear carb
Min. :0.0000 Min. :3.000 Min. :1.000
1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
Median :0.0000 Median :4.000 Median :2.000
Mean :0.4062 Mean :3.688 Mean :2.812
3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
Max. :1.0000 Max. :5.000 Max. :8.000
> #display rows and columns
> dim(mtcars)
[1] 32 11
> #display column names
> names(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
[11] "carb"
> #create histogram of values for mpg
> hist(mtcars$mpg,
+ col='steelblue',
+ main='Histogram',
+ xlab='mpg',
+ ylab='Frequency')
>
30
31