SML Practical 1to11
SML Practical 1to11
CODE:
num1 <- 5.7
comp1 <- 3 + 4i
comp2 <- 1 - 2i
cat("Numeric Numbers:\n")
cat("Integer Numbers:\n")
cat("Complex Numbers:\n")
cat("Integer to Numeric:\n")
cat("Numeric to Complex:\n")
cat("Addition Results:\n")
cat("Subtraction Results:\n")
OUTPUT:
PRACTICAL NO 3:
CODE:
data(mtcars)
print(mtcars)
str(mtcars)
print(var_names)
print(mpg_values)
print(sorted_mpg_values)
summary(mtcars)
OUTPUT:
PRACTICAL NO 4:
CODE:
data(mtcars)
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")
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))
# 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
Code:-
*Program:-
# Load necessary libraries
library(ggplot2)
# 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: