0% found this document useful (0 votes)
26 views

Prerequisites: R Installation

Uploaded by

rohitseth9454
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)
26 views

Prerequisites: R Installation

Uploaded by

rohitseth9454
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/ 11

Prerequisites:

1. R Installation:
If you don't have R installed, follow these steps:

o Download R from the official site: https://cran.r-project.org/.

o Install RStudio (an IDE for R): https://www.rstudio.com/.

2. Necessary Packages:
For the tasks, you will need the following R packages:

o ggplot2

o stats

o caret

o arules

Install them using: R

install.packages(c("ggplot2", "stats", "caret", "arules"))


Task 1: Numerical Operations in R

To get input from the user and perform numerical operations like MAX, MIN, AVG, SUM, SQRT, and
ROUND.

Code: R

# Input numbers from user

nums <- as.numeric(unlist(strsplit(readline(prompt="Enter numbers separated by commas: "), ",")))

# Perform operations

max_val <- max(nums)

min_val <- min(nums)

avg_val <- mean(nums)

sum_val <- sum(nums)

sqrt_vals <- sqrt(nums)

rounded_vals <- round(nums)

# Output results

cat("Maximum:", max_val, "\n")

cat("Minimum:", min_val, "\n")

cat("Average:", avg_val, "\n")

cat("Sum:", sum_val, "\n")

cat("Square roots:", sqrt_vals, "\n")

cat("Rounded values:", rounded_vals, "\n")


Task 2: Import/Export Data in R

To perform data import/export operations for .CSV, .XLS, and .TXT formats using data frames in R.

Code:R

# CSV import and export

data_csv <- read.csv("input.csv")

write.csv(data_csv, "output.csv")

# TXT import and export

data_txt <- read.table("input.txt", header=TRUE)

write.table(data_txt, "output.txt")

# XLS import and export requires the "readxl" package

install.packages("readxl")

library(readxl)

data_xls <- read_excel("input.xls")

write.csv(data_xls, "output_xls.csv") # R does not directly export to xls


Task 3: Matrix Operations in R

To perform matrix operations like addition, subtraction, multiplication, and division.

Code:R

# Input matrices

matrix1 <- matrix(as.numeric(unlist(strsplit(readline(prompt="Enter matrix 1 elements: "), ","))),


nrow=3, ncol=3)

matrix2 <- matrix(as.numeric(unlist(strsplit(readline(prompt="Enter matrix 2 elements: "), ","))),


nrow=3, ncol=3)

# Perform operations

addition <- matrix1 + matrix2

subtraction <- matrix1 - matrix2

multiplication <- matrix1 %*% matrix2

division <- matrix1 / matrix2 # Element-wise division

transpose1 <- t(matrix1)

# Output results

cat("Addition:\n", addition, "\n")

cat("Subtraction:\n", subtraction, "\n")

cat("Multiplication:\n", multiplication, "\n")

cat("Division:\n", division, "\n")

cat("Transpose of matrix1:\n", transpose1, "\n")


Task 4: Statistical Operations in R

To calculate Mean, Median, Mode, and Standard Deviation.

Code:R

# Input numbers

nums <- as.numeric(unlist(strsplit(readline(prompt="Enter numbers separated by commas: "), ",")))

# Calculate statistics

mean_val <- mean(nums)

median_val <- median(nums)

mode_val <- as.numeric(names(sort(table(nums), decreasing=TRUE)[1])) # Mode using table

sd_val <- sd(nums)

# Output results

cat("Mean:", mean_val, "\n")

cat("Median:", median_val, "\n")

cat("Mode:", mode_val, "\n")

cat("Standard Deviation:", sd_val, "\n")


Task 5: Data Preprocessing in R (Missing Values and Normalization)

To handle missing data and perform Min-Max normalization.

Code: R

# Example dataset with missing values

data <- data.frame(x=c(1, 2, NA, 4, 5), y=c(NA, 3, 6, 7, 8))

# Handling missing data by replacing with mean

data[is.na(data)] <- mean(data, na.rm=TRUE)

# Min-Max Normalization

normalized_data <- as.data.frame(lapply(data, function(x) (x - min(x)) / (max(x) - min(x))))

# Output results

cat("Data after handling missing values:\n", data, "\n")

cat("Normalized data:\n", normalized_data, "\n")


Task 6: PCA for Dimensionality Reduction

Perform PCA on the Houses dataset.

Code: R

# Using the iris dataset for example

data(iris)

iris_pca <- prcomp(iris[, 1:4], center=TRUE, scale.=TRUE)

# Output PCA result

summary(iris_pca)

plot(iris_pca)
Task 7: Simple Linear Regression in R

To perform simple linear regression.

Code: R

# Using built-in mtcars dataset

model <- lm(mpg ~ wt, data=mtcars)

summary(model)

plot(mtcars$wt, mtcars$mpg)

abline(model)
Task 8: K-Means Clustering

K-means clustering and visualization on the Iris dataset.

Code: R

data(iris)

kmeans_result <- kmeans(iris[, 1:4], centers=3)

# Plotting the clusters

plot(iris$Sepal.Length, iris$Sepal.Width, col=kmeans_result$cluster)


Task 9: Disease Diagnosis using KNN

KNN classification to diagnose disease and plot results.

Code: R

# Using the iris dataset as an example

library(caret)

set.seed(123)

trainIndex <- createDataPartition(iris$Species, p=0.7, list=FALSE)

trainData <- iris[trainIndex, ]

testData <- iris[-trainIndex, ]

model_knn <- train(Species ~ ., data=trainData, method="knn",


trControl=trainControl(method="cv"))

pred_knn <- predict(model_knn, newdata=testData)

confusionMatrix(pred_knn, testData$Species)
Task 10: Market Basket Analysis using Apriori Algorithm

To perform market basket analysis using association rules.

Code: R

library(arules)

data("Groceries")

rules <- apriori(Groceries, parameter=list(supp=0.01, conf=0.5))

inspect(head(rules))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy