MQP R Answers
MQP R Answers
MQP R Answers
Q1 (a): Explain with example Environments and Func ons (12 Marks)
Environments in R
An environment is a collec on of paired objects, such as variables or func ons, and their
associated values. It is where R looks up the values associated with variable names during
execu on.
1. Parent Environment: Every environment has a parent, except for the empty
environment.
2. Search Path: R searches through environments in the order defined by the parent
structure when resolving variable names.
Example of Environments:
x <- 10
x <- 5
Func ons in R
A func on is a block of code that performs a specific task. Func ons are first-class objects in
R, meaning they can be treated like any other object.
result <- a + b
return(result)
Q1 (b): Explain with example flow control and loops in R programming (8 Marks)
Flow Control
Flow control in R determines the logical flow of execu on. The main constructs are:
1. If-Else Statements
2. Switch Statements
Example of If-Else:
x <- 10
if (x > 0) {
print("Posi ve number")
} else {
print("Non-posi ve number")
Example of Switch:
"Unknown fruit")
print(result)
Loops
Loops are used to repeat ac ons. Common loops in R include:
for (i in 1:5) {
print(i)
count <- 1
1. Numeric:
2. Integer:
o Example: y <- 5L
3. Character:
o Enclosed in quotes.
o Example: z <- "Hello"
4. Logical:
5. Complex:
o Example: c <- 2 + 3i
6. Raw:
Code Example:
comp <- 2 + 3i
o Choose the appropriate version for your opera ng system and install.
2. Install an IDE:
o Get familiar with R's Console, Environment, and Script Editor in RStudio.
Example Session:
install.packages("dplyr")
library(dplyr)
print("Welcome to R!")
MODULE-2
Q3 (a): Explain Lists and Data Frames with examples (10 Marks)
Lists
A list in R is a collec on of elements that can hold mul ple types of data (e.g., numeric,
character, logical). It is a flexible data structure and can even store other lists or data frames.
Features of Lists:
Accessed using [[ ]] or $.
Example of Lists:
# Crea ng a list
my_list <- list(name = "John", age = 25, scores = c(90, 85, 88))
# Accessing elements
Data Frames
A data frame is a table-like structure where each column can have a different type of data,
but all rows must have the same number of elements. It is the most common data structure
for storing datasets.
df <- data.frame(
ID = 1:3,
# Accessing elements
Q3 (b): Develop a program to create two 3x3 matrices and perform matrix opera ons (10
Marks)
Solu on:
1. Create Matrices:
Use matrix() to define two 3x3 matrices.
p r in t ( ” Matrix A: ” )
p r in t (A)
p r in t ( ” Matrix B: ” )
p r in t (B)
transpose_B <-t(b)
p r in t ( ” Transpose o f Matrix A: ” )
p r in t ( transpose_A)
p r in t ( ” Transpose o f Matrix B: ” )
p r in t ( transpose_B)
C <-A+B
p r in t (C)
C <- A-B
p r in t (C)
C <- A %*% B
p r in t ( ” Matrix M u l t ip l i c a t i o n o f A and B: ” )
p r in t (C)
Factors
Factors are used to handle categorical data in R. They store data as levels and are helpful in
sta s cal modeling.
Features of Factors:
Example of Factors:
# Crea ng a factor
print(gender)
Strings
Strings in R represent textual data. They are created using quotes (" or '). R provides
func ons like paste(), substr(), and nchar() to manipulate strings.
Example of Strings:
Q4 (b): Develop an R program to find all prime numbers up to a specified number using
the sieve of Eratosthenes (10 Marks)
Solu on:
sieve_of_eratosthenes <- func on (limit) {
if (limit >= 2) {
for (p in numbers) {
break
}
numbers < numbers [ numbers == p | numbers %% p != 0]
return (numbers)
} else {
print (prime_numbers)
Module 3
Q5 (a): Explain briefly about impor ng and expor ng files (10 Marks)
Impor ng Files in R
Impor ng refers to loading external data into R for analysis. Common func ons for impor ng
files include:
print(head(data))
Expor ng Files in R
Expor ng refers to saving data or results into external files. Common func ons include:
Data Cleaning
Data cleaning ensures the data is free from errors or inconsistencies. Key steps include:
Example:
4. Standardize Data: Normalize numeric columns to bring them into a similar scale.
5. Detect and Handle Outliers: Use visualiza on (boxplots) or sta s cal methods.
Data Transforma on
Data transforma on involves conver ng data into a suitable format for analysis. Steps
include:
5. Filtering and Selec ng Columns: Use dplyr func ons like filter() and select().
Example:
6. library(dplyr)
Q6 (a): Develop a data form in R to store 20 employee details and analyze (10 Marks)
Solu on:
Code:
Program no 9
R provides tools to connect to and interact with databases, such as MySQL, SQLite, or
PostgreSQL. The DBI and RSQLite packages are commonly used for database opera ons.
# Load libraries
library(DBI)
library(RSQLite)
# Create a table
print(query_result)
# Disconnect
dbDisconnect(con)
Module 4
Q7 (a): Explain briefly Exploratory Data Analysis (EDA) and Sca er Plots (10 Marks)
o Use func ons like str(), summary(), and head() to inspect data.
Example:
summary(mtcars)
Sca er Plots
Sca er plots visualize rela onships between two numeric variables. Each point represents
an observa on.
Key Features:
Example:
plot(mtcars$wt, mtcars$mpg,
col = "blue",
pch = 19)
Q7 (b): Explain base graphics and la ce graphics with the help of box plots (10 Marks)
Base Graphics
Base graphics use simple plo ng func ons for visualiza ons. They are part of R's core
func onality.
xlab = "Cylinders",
La ce Graphics
La ce is a powerful visualiza on package for crea ng mul variate graphics. It focuses on
condi oning (subse ng) data for plots.
library(la ce)
xlab = "Cylinders",
Q8 (a): Demonstrate progression of salary with years of experience using lm() (10 Marks)
Steps:
Code:
# Create dataset
years_of_experience = 1:10,
salary = c(30000, 35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000, 75000)
plot(data$years_of_experience, data$salary,
ylab = "Salary",
col = "blue",
pch = 19)
# Save coefficients
write.csv(coef(model), "coefficients.csv")
write.csv(data, "updated_data.csv")
Q8 (b): Explain histograms in base graphics, la ce graphics, and ggplot2 (10 Marks)
Histograms
Histograms visualize the distribu on of a single variable by dividing it into bins.
1. Base Graphics:
Use the hist() func on.
Example:
4. La ce Graphics:
Use the histogram() func on.
Example:
5. library(la ce)
9. ggplot2:
Use geom_histogram().
Example:
10. library(ggplot2)
13. labs( tle = "Histogram of MPG (ggplot2)", x = "Miles Per Gallon", y = "Count")
Module 5
Q9 (a): Explain briefly basic sta s cal measures available in R (10 Marks)
R provides several basic sta s cal func ons to summarize data and understand its
characteris cs.
4. print(mean_val) # Output: 30
7. print(median_val) # Output: 30
12. }
17. print(sd_val)
18. Variance: The square of the standard devia on.
Func on: var()
Example:
20. print(var_val)
21. Range: The difference between the maximum and minimum values.
Func on: range()
Example:
25. summary(data)
Q9 (b): Explain four in-built func ons to generate normal distribu on in R (10 Marks)
The normal distribu on is a con nuous probability distribu on. R provides func ons to work
with it:
1. dnorm(): Calculates the density (height of the probability density func on) at a point.
Example:
12. print(random_numbers)
Q10 (a): Explain Correla on Analysis and Linear Regression (10 Marks)
Correla on Analysis
Correla on measures the strength and direc on of the rela onship between two variables.
2. Nega ve Correla on: One variable increases while the other decreases.
x <- c(1, 2, 3, 4, 5)
Linear Regression
Linear regression models the rela onship between a dependent variable (y) and one or
more independent variables (x).
2. Interpret coefficients.
Example:
# Dataset
x = c(1, 2, 3, 4, 5),
y = c(2, 4, 6, 8, 10)
summary(model)
Types of ANOVA:
Example:
# Dataset
score = c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
# Perform ANOVA
summary(anova_result)
Interpreta on:
If the p-value < 0.05, at least one group mean is significantly different from others.