0% found this document useful (0 votes)
25 views23 pages

SML Practical 1to11

Uploaded by

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

SML Practical 1to11

Uploaded by

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

Practical no 2:

Q) Write Program to-


a. Demonstrate the use of R-Numbers. (numeric, integer, complex).
b. Convert number from one type to other using functions.
c. Perform following operations.
i. Addition and Subtraction on numbers.
ii. Find Square root using of number using built-in function.

CODE:
num1 <- 5.7

num2 <- 3.2

int1 <- as.integer(4)

int2 <- as.integer(2)

comp1 <- 3 + 4i

comp2 <- 1 - 2i

cat("Numeric Numbers:\n")

cat("num1:", num1, "\n")

cat("num2:", num2, "\n\n")

cat("Integer Numbers:\n")

cat("int1:", int1, "\n")

cat("int2:", int2, "\n\n")

cat("Complex Numbers:\n")

cat("comp1:", comp1, "\n")

cat("comp2:", comp2, "\n\n")

num_to_int <- as.integer(num1)


cat("Numeric to Integer:\n")

cat("num_to_int:", num_to_int, "\n\n")

int_to_num <- as.numeric(int1)

cat("Integer to Numeric:\n")

cat("int_to_num:", int_to_num, "\n\n")

num_to_comp <- as.complex(num1)

cat("Numeric to Complex:\n")

cat("num_to_comp:", num_to_comp, "\n\n")

comp_to_num <- Re(comp1)

cat("Complex to Numeric (real part only):\n")

cat("comp_to_num:", comp_to_num, "\n\n")

add_numeric <- num1 + num2

add_integer <- int1 + int2

add_complex <- comp1 + comp2

cat("Addition Results:\n")

cat("Addition of numeric numbers:", add_numeric, "\n")

cat("Addition of integers:", add_integer, "\n")

cat("Addition of complex numbers:", add_complex, "\n\n")

sub_numeric <- num1 - num2

sub_integer <- int1 - int2

sub_complex <- comp1 - comp2

cat("Subtraction Results:\n")

cat("Subtraction of numeric numbers:", sub_numeric, "\n")

cat("Subtraction of integers:", sub_integer, "\n")


cat("Subtraction of complex numbers:", sub_complex, "\n\n")

sqrt_numeric <- sqrt(num1)

sqrt_integer <- sqrt(as.numeric(int1))

sqrt_complex <- sqrt(comp1)

cat("Square Root Results:\n")

cat("Square root of numeric number:", sqrt_numeric, "\n")

cat("Square root of integer:", sqrt_integer, "\n")

cat("Square root of complex number:", sqrt_complex, "\n")

OUTPUT:
PRACTICAL NO 3:

Write Program to-


a. Print any built-in data set of R.
b. Get information about the data set.
c. Find the dimensions of the data set and view the names of the variables.
Hint: Use dim( ) and names( ) function.
d. Find the name of each row in the first column.
Hint: Use the rownames( ) function.
e. Print all values that belong to a variable.
f. Sort the values of variable.
g. Get the statistical summary of the data.

CODE:
data(mtcars)

cat("Built-in Data Set (mtcars):\n")

print(mtcars)

cat("\nInformation about the dataset:\n")

str(mtcars)

cat("\nDimensions of the dataset:\n")

dims <- dim(mtcars)

print(dims) # prints number of rows and columns

cat("\nNames of the variables:\n")

var_names <- names(mtcars)

print(var_names)

cat("\nNames of each row in the first column:\n")

row_names <- rownames(mtcars)


print(row_names)

cat("\nValues of the variable 'mpg':\n")

mpg_values <- mtcars$mpg

print(mpg_values)

cat("\nSorted values of the variable 'mpg':\n")

sorted_mpg_values <- sort(mpg_values)

print(sorted_mpg_values)

cat("\nStatistical summary of the dataset:\n")

summary(mtcars)

OUTPUT:
PRACTICAL NO 4:

Write a program to-


a. Find the lowest or highest value in a data set.
Hint: Use min( ) and max( ) functions.
b. Find the index position of the max and min value in the table.
Hint: use which.max( ) and which.min( ) functions.

CODE:

data(mtcars)

mpg_values <- mtcars$mpg

lowest_value <- min(mpg_values)

highest_value <- max(mpg_values)

cat("Lowest value in 'mpg':", lowest_value, "\n")

cat("Highest value in 'mpg':", highest_value, "\n")

index_of_max <- which.max(mpg_values)

index_of_min <- which.min(mpg_values)

cat("Index of highest value in 'mpg':", index_of_max, "\n")

cat("Index of lowest value in 'mpg':", index_of_min, "\n")

cat("Car model with highest 'mpg':", rownames(mtcars)[index_of_max],


"\n")

cat("Car model with lowest 'mpg':", rownames(mtcars)[index_of_min], "\n")


OUTPUT:
PRACTICAL NO.5
*QUESTIONS:
Write programs to calculate Measures of Central tendency.
a. Import data into R.
b. Calculate the Mean (Average value) of a variable from the given data set.
c. Find the Median (Mid-Point value) of the variable from the given data set.
d. Calculate the mode for the variable from the given data set.( by sorting the
column of the dataframe and by using the ‘modest’ package).

e. Calculate the Percentile of the variable from the given data set.

*PROGRAMME:
# Install necessary packages if they are not already installed
install.packages("dplyr")
install.packages("modest")

# Load necessary libraries


library(dplyr)
library(modest) # For calculating mode

# a. Import data into R


# For demonstration purposes, we'll create a sample data frame.
# Replace this with your actual data import code, e.g., read.csv("file.csv")

df <- data.frame(
value = c(10, 20, 20, 30, 40, 50, 60, 70, 70, 80, 90, 100)
)
# b. Calculate the Mean (Average value) of a variable from the given data set
mean_value <- mean(df$value)
print(paste("Mean:", mean_value))

# c. Find the Median (Mid-Point value) of the variable from the given data set
median_value <- median(df$value)
print(paste("Median:", median_value))

# d. Calculate the Mode for the variable from the given data set
# By sorting the column
sorted_values <- sort(df$value)
mode_sorted <- as.numeric(names(sort(table(sorted_values), decreasing =
TRUE)[1]))
print(paste("Mode (sorted):", mode_sorted))

# By using the 'modest' package


mode_modest <- modest::mode(df$value)
print(paste("Mode (modest):", mode_modest))

# e. Calculate the Percentile of the variable from the given data set
# Define percentiles to calculate, e.g., the 25th, 50th, and 75th percentiles
percentiles <- quantile(df$value, probs = c(0.25, 0.50, 0.75))
print("Percentiles:")
print(percentiles)
*OUTPUT:
PRACTICAL NO.6

*QUESTIONS:
Write programs to-
a. Print Original Data Frame, Modified Frequency Table, Cumulative Frequency
Table, Relative Frequency Table.
b. Create the Frequency Table by using multiple arguments.
c. Plot the frequency table using ggplot function.

*PROGRAMME:

library(ggplot2)
>
> # Example data frame
> df <- data.frame(
+ item = c("Apple", "Banana", "Apple", "Orange", "Banana", "Apple", "A
pple", "Orange", "Banana", "Banana"),
+ region = c("North", "South", "North", "East", "West", "South", "Nort
h", "East", "South", "East")
+ )
>
> # a. Print Original Data Frame, Modified Frequency Table, Cumulative Fre
quency Table, Relative Frequency Table
>
> # Print the Original Data Frame
> print("Original Data Frame:")
[1] "Original Data Frame:"
> print(df)
item region
1 Apple North
2 Banana South
3 Apple North
4 Orange East
5 Banana West
6 Apple South
7 Apple North
8 Orange East
9 Banana South
10 Banana East
>
> # Modified Frequency Table
> freq_table <- df %>%
+ count(item) %>%
+ arrange(desc(n))
> print("Modified Frequency Table:")
[1] "Modified Frequency Table:"
> print(freq_table)
item n
1 Apple 4
2 Banana 4
3 Orange 2
>
> # Cumulative Frequency Table
> cumulative_freq_table <- freq_table %>%
+ mutate(cumulative_freq = cumsum(n))
> print("Cumulative Frequency Table:")
[1] "Cumulative Frequency Table:"
> print(cumulative_freq_table)
item n cumulative_freq
1 Apple 4 4
2 Banana 4 8
3 Orange 2 10
>
> # Relative Frequency Table
> relative_freq_table <- freq_table %>%
+ mutate(relative_freq = n / sum(n))
> print("Relative Frequency Table:")
[1] "Relative Frequency Table:"
> print(relative_freq_table)
item n relative_freq
1 Apple 4 0.4
2 Banana 4 0.4
3 Orange 2 0.2
>
> # b. Create the Frequency Table by Using Multiple Arguments
>
> # Frequency Table by item and region
> freq_table_mult <- df %>%
+ count(item, region) %>%
+ arrange(item, region)
> print("Frequency Table with Multiple Arguments:")
[1] "Frequency Table with Multiple Arguments:"
> print(freq_table_mult)
item region n
1 Apple North 3
2 Apple South 1
3 Banana East 1
4 Banana South 2
5 Banana West 1
6 Orange East 2
>
> # c. Plot the Frequency Table Using ggplot Function
>
> # Plotting the Modified Frequency Table
> ggplot(freq_table, aes(x = reorder(item, n), y = n)) +
+ geom_bar(stat = "identity", fill = "skyblue") +
+ labs(x = "Item", y = "Frequency", title = "Frequency of Each Item")
+
+ theme_minimal()
>
> # Plotting the Frequency Table with Multiple Arguments
> ggplot(freq_table_mult, aes(x = reorder(item, n), y = n, fill = region))
+
+ geom_bar(stat = "identity", position = "dodge") +
+ labs(x = "Item", y = "Frequency", title = "Frequency of Items by Reg
ion") +
+ theme_minimal()
*OUTPUT:
Practical no :- 7

*Write programs to calculate-Variance, Standard Deviation, Range, Mean


Deviation for the given data.

Code:-

> data <- c(5, 7, 3, 9, 2)


> mean_data <- mean(data)
> variance <- var(data)
> std_deviation <- sd(data)
> range_data <- range(data)
> range_value <- diff(range_data)
> mean_deviation <- mean(abs(data - mean_data))
> cat("Mean:", mean_data, "\n")
Mean: 5.2
> cat("Variance:", variance, "\n")
Variance: 8.2
> cat("Standard Deviation:", std_deviation, "\n")
Standard Deviation: 2.863564
> cat("Range:", range_value, "\n")
Range: 7
> cat("Mean Deviation:", mean_deviation, "\n")
Mean Deviation: 2.24
Output:-
PARCTICAL NUMBER:-10
*Question:
Write a Program to draw a scatterplot for two variables for the given
dataset

*Program:-
# Load necessary libraries
library(ggplot2)

# Load the dataset


data <- read.csv('data.csv')

# Create a scatterplot
ggplot(data, aes(x = x, y = y)) +
geom_point(color = 'blue', alpha = 0.5) +
ggtitle('Scatterplot of x vs. y') +
xlab('x') +
ylab('y') +
theme_minimal()

*Output:
PRACTICAL NO.11
*Questions:
Write Program to perform the correlation test to evaluate the association between two or
more variables.
a. Install and load required R packages.
b. Compute correlation in R.
c. Visualize your data using scatter plots.
d. Perform Preliminary test to check the test assumptions.

*Programme:
# Sample data
> x <- c(1, 2, 3, 4, 5) # Example data for variable x
> y <- c(2, 4, 6, 8, 10) # Example data for variable y
>
> # Compute the correlation coefficient
> correlation <- cor(x, y)
> cat("Correlation coefficient between x and y:", correlation, "\n")
Correlation coefficient between x and y: 1
>
> # Create a scatter plot
> plot(x, y, main = "Scatter Plot of x vs y", xlab = "x", ylab = "y", pch = 19, col = "blue")
>
> # Fit a linear model
> model <- lm(y ~ x)
>
> # Add the regression line to the scatter plot
> abline(model, col = "red")
>
> # Plot residuals to check homoscedasticity
> residuals <- model$residuals
> plot(residuals, main = "Residuals Plot", xlab = "Index", ylab = "Residuals", pch = 19, col =
"green")
> abline(h = 0, col = "red")
>
> # Display summary of the linear model to check assumptions
> cat("Summary of the linear model:\n")
Summary of the linear model:
> summary(model)

*Output:

You might also like