RStudio
RStudio
Aim:
Algorithm:
R Code:
# Create objects
x = 10
y = "Hello"
z = x + 20
# Print objects
print(x)
print(y)
print(z)
# Remove an object
rm(x)
# Confirm removal
cat("Objects after removing 'x':", ls(), "\n")
Output Example:
[1] 10
[1] "Hello"
[1] 30
Class of x: numeric
Type of y: character
Objects in memory: y z
Objects after removing 'x': y z vec
[1] 1 2 3 4 5
[1] 1 4 9 16 25
Algorithm:
R Code:
# Modify a column
students$Marks = students$Marks + 5
cat("\nData Frame after increasing marks by 5:\n")
print(students)
Output Example:
R Code:
cat("\nMatrix 2:\n")
print(matrix2)
# Matrix addition
matrix_sum <- matrix1 + matrix2
cat("\nMatrix Addition (Matrix 1 + Matrix 2):\n")
print(matrix_sum)
# Transpose of a matrix
transpose_matrix <- t(matrix1)
cat("\nTranspose of Matrix 1:\n")
print(transpose_matrix)
Output Example:
Matrix 1:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Matrix 2:
[,1] [,2] [,3]
[1,] 6 4 2
[2,] 5 3 1
Transpose of Matrix 1:
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
Determinant of Matrix 3:
[1] -2
Inverse of Matrix 3:
[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5
This program demonstrates how to create matrices, perform basic arithmetic operations,
transpose, find determinants, and calculate inverses in R.
To explore and demonstrate the use of various built-in functions in R for mathematical,
statistical, and data manipulation tasks.
Algorithm:
R Code:
# 1. Mathematical Functions
x <- 16
y <- -4
cat("Square root of", x, ":", sqrt(x), "\n")
cat("Absolute value of", y, ":", abs(y), "\n")
cat("Natural logarithm of", x, ":", log(x), "\n")
cat("Exponential of", y, ":", exp(y), "\n\n")
# 2. Statistical Functions
data <- c(10, 20, 30, 40, 50)
cat("Mean of data:", mean(data), "\n")
cat("Median of data:", median(data), "\n")
cat("Standard deviation of data:", sd(data), "\n")
cat("Variance of data:", var(data), "\n")
cat("Summary of data:\n")
print(summary(data))
cat("\n")
# 3. Character Functions
text <- "Hello R"
cat("Uppercase:", toupper(text), "\n")
cat("Lowercase:", tolower(text), "\n")
cat("Substring (1 to 5):", substr(text, 1, 5), "\n")
cat("Concatenate strings:", paste("Learning", "R", sep = " "), "\n\
n")
# 5. Aggregation
df <- data.frame(
Category = c("A", "A", "B", "B", "C"),
Value = c(10, 15, 10, 20, 30)
)
aggregated <- aggregate(Value ~ Category, data = df, sum)
cat("Aggregated values by category:\n")
print(aggregated)
Output Example:
Square root of 16 : 4
Absolute value of -4 : 4
Natural logarithm of 16 : 2.772589
Exponential of -4 : 0.018316
Mean of data: 30
Median of data: 30
Standard deviation of data: 15.81139
Variance of data: 250
Summary of data:
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.0 20.0 30.0 30.0 40.0 50.0
Uppercase: HELLO R
Lowercase: hello r
Substring (1 to 5): Hello
Concatenate strings: Learning R
Generated sequence: 1 3 5 7 9
Repeated values: 5 5 5 5
This program demonstrates the use of various built-in functions in R for handling
mathematical operations, statistical analysis, character string manipulations, sequence
generation, and data aggregation.
To demonstrate the import and export of data files in R, such as CSV, Excel, and text files,
and perform basic operations on the imported data.
Algorithm:
R Code:
# 3. Modify Data
cat("\nModifying data: Adding a new column...\n")
data$NewColumn <- data$ExistingColumn * 2 # Replace ExistingColumn
with an actual column name
library(readxl)
library(writexl)
Output Example:
This program demonstrates importing and exporting CSV, Excel, and text files in R, with
basic operations performed on the imported data.
Algorithm:
R Code:
cat("Dataset:\n")
print(data)
Dataset:
x y
1 5 12
2 10 20
3 15 28
4 20 36
5 25 44
Residuals:
1 2 3 4 5
-1.421e-14 -7.105e-15 0.000e+00 7.105e-15 1.421e-14
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.000 5.568e-15 7.18e+14 <2e-16 ***
x 1.600 3.712e-16 4.31e+15 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
● A scatter plot is displayed with data points (x vs. y) in blue and the regression line in
red.
This program demonstrates how to calculate statistical measures, evaluate correlations, and
perform regression analysis, along with visualizing results in R.
Expected Output:
Confusion Matrix:
Actual
Predicted setosa versicolor virginica
setosa 15 0 0
versicolor 0 14 1
virginica 0 0 15
Conclusion:
The KNN algorithm was successfully implemented, and the accuracy of the model was
calculated. This demonstrates the effectiveness of KNN for classification tasks.
Algorithm:
R Code:
library(forecast)
library(ggplot2)
Expected Output:
Coefficients:
ma1 sma1
-0.401 -0.627
s.e. 0.088 0.076
Forecasted Values:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 1961 443.961 421.3156 466.6064 409.3448 478.5772
Feb 1961 444.450 421.9786 466.9214 409.9040 478.9960
...
Accuracy Metrics:
ME RMSE MAE MPE MAPE
Training set 0.1234 35.67891 28.2345 -0.0234 3.67890
Visualizations:
Conclusion:
The time series analysis was successfully performed, including decomposition and
forecasting using ARIMA. The forecasted values provide insights into future trends.
To demonstrate a basic data mining algorithm, such as association rule mining using the
Apriori algorithm in R.
Algorithm:
library(arules)
Expected Output:
Visualizations:
1. Graph Plot:
○ Displays items and association rules in a network format.
2. Scatter Plot (Optional):
○ Shows the relationship between support, confidence, and lift.
Conclusion:
The Apriori algorithm was successfully implemented to discover frequent itemsets and
generate association rules. This demonstrates the basic principles of data mining and
association rule learning in R.
To implement text mining using R by preprocessing textual data and extracting insights such
as frequent terms or word clouds.
Algorithm:
R Code:
library(tm)
library(wordcloud)
library(SnowballC)
Expected Output:
A colorful word cloud showing frequent terms like "text," "data," and "mine."
Conclusion:
Text mining was successfully performed using R. Preprocessing techniques and analysis,
including generating a word cloud, helped extract meaningful insights from textual data.
Experiment 11: Data Visualization Techniques
Aim:
To demonstrate various data visualization techniques in R using basic and advanced plots.
Algorithm:
R Code:
library(ggplot2)
Expected Output:
1. Bar Plot:
○ Displays the frequency of cars based on the number of cylinders.
2. Scatter Plot:
○ Shows the relationship between miles per gallon (MPG) and horsepower
(HP).
3. Histogram:
○ Represents the distribution of MPG across the dataset.
4. Box Plot:
○ Compares MPG values for different cylinder categories.
5. Advanced Scatter Plot:
○ Highlights the relationship between weight and MPG, grouped by the number
of gears.
Conclusion:
Various data visualization techniques were successfully implemented using R. Both basic
and advanced plots provide insights into the dataset, demonstrating the power of visual
analysis.
Algorithm:
R Code:
Expected Output:
T-Test Results:
Welch Two Sample t-test
Visualization:
A boxplot comparing the two groups, showing the difference in their distributions.
Conclusion:
Hypothesis testing was successfully conducted using an independent t-test. The results
indicate whether there is a statistically significant difference between the means of the two
sample groups.