0% found this document useful (0 votes)
0 views18 pages

R Programming Lab

The document outlines a series of experiments designed to teach users how to effectively use R and RStudio for data analysis, covering topics such as setting up the environment, managing packages, working with data frames, data visualization with ggplot2, and performing statistical analysis. Each experiment includes objectives, key features, learning outcomes, and detailed instructions for conducting the experiments. The overall goal is to equip participants with foundational skills in R programming and data manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views18 pages

R Programming Lab

The document outlines a series of experiments designed to teach users how to effectively use R and RStudio for data analysis, covering topics such as setting up the environment, managing packages, working with data frames, data visualization with ggplot2, and performing statistical analysis. Each experiment includes objectives, key features, learning outcomes, and detailed instructions for conducting the experiments. The overall goal is to equip participants with foundational skills in R programming and data manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

EXPERIMENT NO.

EXPERIMENT TITLE MAPPED CO/PO

1 Setting Up and Exploring RStudio Environment CO1

2 Installing and Configuring R Packages CO1, CO2

3 Working with Data Frames and File Handling in R CO1

4 Managing User Access and Permissions in R (Data Security) CO1, CO3

5 Creating and Hosting R Markdown Reports and Shiny Apps CO1

6 Using R for Database Management (SQLite, PostgreSQL, MySQL) CO1, CO5

7 Introduction to Functional Programming in R CO2

8 Optimizing Performance with Parallel Computing in R CO2

9 Monitoring System Resources and Performance in R CO8

10 Managing Computational Resources and Memory Optimization in R CO7

Experiment 1

Title: Setting Up and Exploring RStudio Environment

Introduction to Setting Up and Exploring RStudio

RStudio is an integrated development environment (IDE) for R that provides a user-friendly interface to write, run, and debug R scripts. It allows

users to interact with R efficiently, manage projects, and perform data analysis. This experiment introduces beginners to the RStudio interface and

helps them become familiar with navigation and basic functionalities of the platform.

Key Features of RStudio

● Script Editor: A dedicated space to write and execute R scripts.

● Console: Runs R commands interactively.

● Environment Pane: Displays active variables, datasets, and functions.

● Plots Pane: Visual representation of graphs and plots.

● Packages Pane: Manage R packages (install, update, load).

● Help and Viewer Pane: Access documentation, tutorials, and R Markdown files.

Objective

● To understand the structure and components of the RStudio interface.

1
● To explore various R features and functionalities.

● To configure basic R settings and install necessary packages.

Learning Outcomes

By the end of this experiment, participants will be able to:

● Navigate the RStudio interface effectively.

● Access and explore key R functions and libraries.

● Configure RStudio settings, such as working directory and package management.

● Understand the basic structure and functionality of the R programming environment.

Instructions for Conducting the Experiment

Preparation:

● Ensure you have a computer with a stable internet connection.

● Download and install R and RStudio from their official websites.

Login and Overview:

● Open RStudio after installation.

● Familiarize yourself with the Console, Script Editor, Environment Pane, and Plots Pane.

Exploring R Features:

Use the Console to run basic R commands:

CopyEdit

print("Hello, R!")

Open the Script Editor and write a simple R script:

CopyEdit

x <- c(1, 2, 3, 4, 5)

mean(x)

Configuring RStudio Settings:

● Go to Tools > Global Options to update RStudio preferences.

Set the working directory using:

2
CopyEdit

setwd("C:/Users/YourName/Documents/R_Projects")

Navigating the Environment Pane:

Create variables and datasets, then observe them in the Environment Pane:

CopyEdit

data <- data.frame(Name = c("Alice", "Bob"), Score = c(90, 85))

View(data)

Installing and Managing Packages:

Use the Packages Pane or install a package using R commands:

CopyEdit

install.packages("ggplot2")

library(ggplot2)

Exploring Help and Documentation:

Use the Help Pane to find documentation:

CopyEdit

?mean

help("lm")

Visualizing Data in the Plots Pane:

Generate a simple plot and observe it in the Plots Pane:

CopyEdit

plot(x, main = "Simple Scatter Plot")

Expected Outcome

Participants will have a foundational understanding of the RStudio Environment, enabling them to navigate, configure, and explore R

programming tools effectively.

3
**************

Experiment 2

Title: Installing and Configuring R Packages

Introduction to Installing and Configuring R Packages

R packages extend the functionality of R by providing additional functions, datasets, and tools for various tasks such as data

visualization, statistical analysis, and machine learning. The Comprehensive R Archive Network (CRAN) hosts thousands of R

packages that users can install and use.

This experiment introduces beginners to installing, loading, updating, and managing R packages within RStudio.

Key Features of R Packages

● CRAN Repository: A vast collection of R packages for different applications.

● Installation and Loading: Install and activate packages for use in R scripts.

● Package Dependencies: Automatically manages required libraries.

● Updating and Removing Packages: Ensures up-to-date functions and security fixes.

● Built-in Documentation: Helps users understand package functions and usage.

Objective

● To understand how to install, load, update, and manage R packages.

● To explore essential R packages for data science, visualization, and analysis.

● To configure RStudio settings for efficient package management.

Learning Outcomes

By the end of this experiment, participants will be able to:

● Install and load packages from CRAN and external repositories.

● Manage package dependencies and resolve conflicts.

● Update, remove, and reinstall R packages.

● Utilize package documentation for effective usage.

Instructions for Conducting the Experiment

Preparation:

● Ensure R and RStudio are installed and working correctly.

● Verify an active internet connection for downloading packages.

4
Installing Packages from CRAN:

1. Open RStudio and go to the Console Pane.

Install a package using the install.packages() function:

CopyEdit

install.packages("tidyverse") # Installs multiple data science packages

2.

Load the installed package using library():

CopyEdit

library(tidyverse) # Loads the package for use

3.

Installing Packages from GitHub or Other Repositories:

● Some R packages are available on GitHub instead of CRAN.

To install them, first install the devtools package:

CopyEdit

install.packages("devtools")

library(devtools)

Then install a GitHub package, for example:

CopyEdit

devtools::install_github("rstudio/shiny") # Installs the Shiny web framework

Exploring Installed Packages:

List all installed packages:

CopyEdit

installed.packages()

5
Check if a package is installed:

CopyEdit

any(grepl("ggplot2", installed.packages()))

Get package details:

CopyEdit

packageVersion("ggplot2")

Updating and Removing Packages:

Update all installed packages:

CopyEdit

update.packages(ask = FALSE)

Remove a package if no longer needed:

CopyEdit

remove.packages("tidyverse")

Using Package Documentation and Help:

Get package documentation:

CopyEdit

help(package = "ggplot2")

View a function's help page:

CopyEdit

?ggplot

6
Get example code for a function:

CopyEdit

example("lm")

Expected Outcome

Participants will have a solid understanding of R package management, including installation, loading, updating, removal, and

documentation usage, enabling them to work efficiently in data science and statistical computing.

**************

Experiment 3

Title: Working with Data Frames and File Handling in R

Introduction to Working with Data Frames and File Handling in R

Data frames are one of the most commonly used data structures in R for storing and analyzing tabular data. R provides built-in

functions for creating, manipulating, and summarizing data frames. Additionally, R supports various file formats such as CSV, Excel,

JSON, and RDS for data import and export.

This experiment introduces students to data frames, how to read and write data files, and perform basic data manipulation in

R.

Key Features of Data Frames in R

● Tabular Data Representation: Stores data in rows and columns similar to Excel or SQL tables.

● Data Import and Export: Supports CSV, Excel, JSON, and native R formats.

● Manipulation Functions: Filtering, sorting, subsetting, and transforming data.

● Summary and Analysis: Calculate means, medians, and standard deviations.

● Integration with Tidyverse: Enhances data manipulation with dplyr and readr packages.

Objective

● To understand the structure and usage of data frames in R.

● To explore different ways of importing and exporting data.

● To apply basic data manipulation techniques in R.

Learning Outcomes

7
By the end of this experiment, participants will be able to:

● Create and modify data frames.

● Import data from CSV, Excel, and JSON files.

● Export processed data to different file formats.

● Perform basic data transformations such as filtering and sorting.

Instructions for Conducting the Experiment

Preparation:

● Ensure R and RStudio are installed.

Install the necessary packages:

CopyEdit

install.packages("tidyverse")

install.packages("readxl") # For Excel file handling

install.packages("jsonlite") # For JSON file handling


● Download or create sample CSV, Excel, and JSON files for testing.

Creating and Exploring a Data Frame:

Create a sample data frame:

CopyEdit

students <- data.frame(

Name = c("Alice", "Bob", "Charlie"),

Age = c(20, 22, 21),

Score = c(85, 90, 78)

View the structure of the data frame:

CopyEdit

str(students)

8
summary(students)

Importing Data from Files:

1. Reading a CSV File:

CopyEdit

data_csv <- read.csv("data/sample.csv")

head(data_csv)

2. Reading an Excel File:

CopyEdit

library(readxl)

data_excel <- read_excel("data/sample.xlsx", sheet = 1)

head(data_excel)

3. Reading a JSON File:

CopyEdit

library(jsonlite)

data_json <- fromJSON("data/sample.json")

print(data_json)

Exporting Data to Files:

1. Writing a CSV File:

9
r

CopyEdit

write.csv(students, "output/students.csv", row.names = FALSE)

2. Writing an Excel File:

CopyEdit

library(writexl)

write_xlsx(students, "output/students.xlsx")

3. Writing a JSON File:

CopyEdit

write(toJSON(students, pretty = TRUE), file = "output/students.json")

Basic Data Manipulation:

1. Selecting Columns:

CopyEdit

students$Name # Extracts the Name column

2. Filtering Rows:

CopyEdit

subset(students, Score > 80)

10
3. Sorting Data:

CopyEdit

students_sorted <- students[order(students$Score, decreasing = TRUE), ]

print(students_sorted)

4. Adding a New Column:

CopyEdit

students$Grade <- ifelse(students$Score >= 85, "A", "B")

print(students)

Expected Outcome

Participants will have a strong foundation in data handling and manipulation in R, enabling them to efficiently import, process,

and export datasets in various formats.

**************

Experiment 4

Title: Data Visualization with ggplot2 in R

Introduction to Data Visualization with ggplot2 in R

Data visualization is a critical aspect of data analysis, as it helps in identifying patterns, trends, and outliers in the data. ggplot2 is

one of the most powerful and flexible R packages for creating visualizations. Based on the Grammar of Graphics, it allows users to

create complex, multi-layered visualizations by combining different components such as data, aesthetics, geometries, and statistics.

This experiment introduces students to the basics of creating visualizations using ggplot2 and explores different types of charts and

plots for data analysis.

11
Key Features of ggplot2

● Grammar of Graphics: Creates visualizations by layering components.

● Customization: Offers extensive customization for themes, colors, labels, and axes.

● Statistical Summaries: Automatically adds statistical summaries (e.g., regression lines, confidence intervals).

● Wide Range of Visualizations: Includes scatter plots, bar charts, histograms, box plots, and more.

● Compatibility with Tidyverse: Integrates well with other tidyverse packages like dplyr and tidyr.

Objective

● To understand the basics of ggplot2 for data visualization.

● To create various types of visualizations such as scatter plots, bar charts, and histograms.

● To customize the appearance of visualizations for better interpretation of data.

Learning Outcomes

By the end of this experiment, participants will be able to:

● Use ggplot2 to create various plots such as scatter plots, bar charts, and histograms.

● Customize visualizations with themes, colors, labels, and titles.

● Apply statistical summaries to the visualizations.

● Interpret visualizations to draw insights from data.

Instructions for Conducting the Experiment

Preparation:

● Ensure R and RStudio are installed.

Install the ggplot2 package if not already installed:

CopyEdit

install.packages("ggplot2")

Load the package in RStudio:

CopyEdit

library(ggplot2)

Creating a Basic Scatter Plot:

12
Use the mtcars dataset (a built-in dataset in R) to create a scatter plot:

CopyEdit

data(mtcars)

ggplot(mtcars, aes(x = wt, y = mpg)) +

geom_point() +

ggtitle("Scatter Plot: Weight vs Miles Per Gallon") +

xlab("Weight (in 1000 lbs)") +

ylab("Miles Per Gallon")

Customizing the Scatter Plot:

Change point colors and size:

CopyEdit

ggplot(mtcars, aes(x = wt, y = mpg)) +

geom_point(color = "blue", size = 3) +

ggtitle("Customized Scatter Plot: Weight vs MPG")

Add a regression line:

CopyEdit

ggplot(mtcars, aes(x = wt, y = mpg)) +

geom_point(color = "red", size = 3) +

geom_smooth(method = "lm", se = FALSE, color = "green") +

ggtitle("Scatter Plot with Regression Line")

Creating a Bar Chart:

Create a bar chart to visualize the frequency of car cylinders (cyl) in the mtcars dataset:

13
CopyEdit

ggplot(mtcars, aes(x = factor(cyl))) +

geom_bar(fill = "skyblue") +

ggtitle("Bar Chart: Count of Cylinders in Cars") +

xlab("Number of Cylinders") +

ylab("Frequency")

Creating a Histogram:

Create a histogram to visualize the distribution of miles per gallon (mpg):

CopyEdit

ggplot(mtcars, aes(x = mpg)) +

geom_histogram(binwidth = 2, fill = "orange", color = "black") +

ggtitle("Histogram: Distribution of Miles Per Gallon") +

xlab("Miles Per Gallon") +

ylab("Frequency")

Creating a Box Plot:

Create a box plot to visualize the distribution of mpg for different cylinder values:

CopyEdit

ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +

geom_boxplot(fill = "lightgreen", color = "darkgreen") +

ggtitle("Box Plot: MPG by Cylinder Count") +

xlab("Number of Cylinders") +

ylab("Miles Per Gallon")

Adding Custom Themes and Labels:

14
Use built-in themes and customize labels for better presentation:

CopyEdit

ggplot(mtcars, aes(x = wt, y = mpg)) +

geom_point(color = "purple", size = 3) +

theme_minimal() +

ggtitle("Scatter Plot with Minimal Theme") +

theme(plot.title = element_text(hjust = 0.5))

Expected Outcome

Participants will be able to create and customize a wide range of visualizations using ggplot2. By the end of the experiment, students

will have the skills to create meaningful plots for analyzing and interpreting data effectively.

**************

Experiment 5

Title: Statistical Analysis and Hypothesis Testing in R

Introduction to Statistical Analysis and Hypothesis Testing in R

Statistical analysis is essential for making informed decisions based on data. R provides a wide range of statistical functions to

perform data analysis, such as calculating descriptive statistics, performing hypothesis testing, and running regression models.

Hypothesis testing is a fundamental concept that allows us to test assumptions and make inferences about data.

In this experiment, students will explore descriptive statistics (mean, median, variance, etc.), perform t-tests, and ANOVA to

assess the significance of different variables. Students will also learn how to perform chi-square tests and work with confidence

intervals for statistical analysis.

Key Features of Statistical Analysis in R

● Descriptive Statistics: Summary measures like mean, median, variance, and standard deviation.

● Hypothesis Testing: t-tests, chi-square tests, ANOVA, etc.

● Confidence Intervals: Calculating the range of values that likely contain the population parameter.

● Regression Analysis: Modeling relationships between variables using linear regression.

15
● p-Values and Significance: Understanding the significance of statistical results.

Objective

● To perform basic statistical analysis using R.

● To apply hypothesis testing techniques such as t-tests and ANOVA.

● To interpret statistical results and make data-driven decisions.

Learning Outcomes

By the end of this experiment, participants will be able to:

● Calculate and interpret descriptive statistics in R.

● Perform t-tests and ANOVA to test hypotheses.

● Interpret p-values and determine statistical significance.

● Apply chi-square tests for categorical data analysis.

Instructions for Conducting the Experiment

Preparation:

● Ensure R and RStudio are installed.

● Download the mtcars dataset (built-in R dataset) or use your own dataset for practice.

Descriptive Statistics:

Calculate the mean, median, variance, standard deviation, and summary statistics:

CopyEdit

data(mtcars)

mean(mtcars$mpg) # Mean

median(mtcars$mpg) # Median

var(mtcars$mpg) # Variance

sd(mtcars$mpg) # Standard Deviation

summary(mtcars$mpg) # Summary statistics

t-Test (One Sample):

Perform a one-sample t-test to compare the mean of miles per gallon (mpg) to a known value (e.g., 20):

16
CopyEdit

t_test_result <- t.test(mtcars$mpg, mu = 20)

t_test_result


● Interpret the p-value and decide whether to reject or fail to reject the null hypothesis.

Independent t-Test (Two Samples):

Compare the mpg of cars with 4 and 6 cylinders using an independent t-test:

CopyEdit

t_test_result_2 <- t.test(mtcars$mpg[mtcars$cyl == 4], mtcars$mpg[mtcars$cyl == 6])

t_test_result_2


● Look at the p-value to determine if there is a significant difference between the groups.

ANOVA (Analysis of Variance):

Perform ANOVA to analyze the differences in mpg across multiple cylinder groups:

CopyEdit

aov_result <- aov(mpg ~ factor(cyl), data = mtcars)

summary(aov_result)


● Check the F-statistic and p-value to assess the significance of the differences.

Chi-Square Test:

Perform a chi-square test to analyze the association between two categorical variables, such as cylinder and gear:

CopyEdit

table_data <- table(mtcars$cyl, mtcars$gear)

chi_square_result <- chisq.test(table_data)

chi_square_result


● Evaluate the chi-square statistic and p-value to check for independence between the variables.

Confidence Interval for Mean:

17
Calculate the 95% confidence interval for the mpg of cars in the dataset:

CopyEdit

conf_int <- t.test(mtcars$mpg)$conf.int

conf_int

Expected Outcome

Participants will have a foundational understanding of how to conduct statistical analysis and hypothesis testing using R. They

will be able to apply t-tests, ANOVA, and chi-square tests to draw conclusions from data. Additionally, participants will be able to

interpret confidence intervals and p-values for statistical inference

**********

18

You might also like