Question Paper 1 Answers (R) by Siddu
Question Paper 1 Answers (R) by Siddu
Module-1
Q1.
iv) 5:9 %% 2
v) C(2, 4 - 2, 1 - 1) - 0
c. Develop an R program to find the factorial of a given number using 5 recursive function
calls.
OR
Q2.
b. Develop R code to calculate the following financial metrics using the data below:
Monthly Revenue: [150, 60, 70, 80, 90, 100, 110, 120, 130, 140, 155, 165]
Monthly Expenses: [30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85]
ii) Profit after tax for each month (tax rate = 30%).
iv) Identify "Good" months (profit after tax > mean profit) and "Bad" months (profit after tax
< mean profit).
v) Identify the "Best" and "Worst" months where profit after tax is maximum and minimum,
respectively.
Module-2
Q3.
a. Develop an R program to create two 3x3 matrices A and B and perform the following
operations:
ii) Addition.
iii) Subtraction.
iv) Multiplication.
i) Creation of lists.
OR
Q4.
ii) Subset the data frame to display items in "Office Supplies" or "Desktop Supplies" category.
iii) Create another data frame called item_details with fields Item Code, ItemQtyOnHand, and
ItemReorderLevel. Merge the two data frames.
Module-3
Q5.
i) with()
ii) within()
iii) order()
b. Design a data frame in R for storing 10 employee details and create a CSV file named
input_CSV containing the details such as ID, Name, Salary, Start Date, and Department.
Perform the following:
i) Find the total number of rows and columns.
OR
Q6.
i) apply()
ii) lapply()
iii) mapply()
iv) rapply()
v) tapply()
(10 Marks)
b. Explain the functions used for importing and exporting unstructured files with examples.
Module - 4
Q7.
a. Write the basic syntax for creating a pie chart, explain each parameter of the function, and
write an R program to create a pie chart for the given flower data:
[Rose: 25, Lotus: 35, Lily: 10, Sunflower: 5, Jasmine: 15]. Create a crested pie chart.
(10 Marks)
b. Explain different ways of creating scatter plots in R.
(10 Marks)
OR
Q8.
a. Demonstrate vertical and horizontal bar plots using graphics with examples.
(10 Marks)
i) hist()
ii) plot()
iii) boxplot()
iv) bwplot()
v) ggplot()
Module - 5
Q9.
a. Define the basic statistical measures: Mean, Median, Mode, Standard Deviation, and
Variance. Develop R code to create a vector x = [45, 56, 78, 12, 3, -91, 45, 15, 1, 24] and
compute these measures.
(10 Marks)
b. What is normal distribution? Explain different types of normal distribution functions built
into R.
(10 Marks)
OR
Q10.
i) Find the correlation between hp (horsepower) and mpg (mileage per gallon) and plot a
graph of hp vs. mpg.
ii) Find the correlation between hp and disp (displacement) and plot a graph of hp vs. disp.
iii) Analyze the correlation between the various columns in the dataset.
Module-1
Q1:
iv) 5:9 %% 2
Explanation: The modulo operator (%%) returns the remainder of division.
Output: [1] 1 0 1 0 1
v) C(2, 4 - 2, 1 - 1) - 0
Explanation: Subtracting 0 does not change the vector.
Output: [1] 2 2 0
b. Explain the basic data types in R with examples:
1. Numeric:
Example: x <- 10.5
2. Integer:
Example: y <- as.integer(3)
3. Complex:
Example: z <- 3 + 4i
4. Logical:
Example: a <- TRUE
5. Character:
Example: s <- "Hello"
if (n <= 1) return(1)
OR Q2:
a. Explain repeat, while, and for loops in R with examples:
1. Repeat Loop:
Example: a <- 1
repeat {
print(a)
a <- a + 1
if (a > 5) break
2. While Loop:
Example: a <- 1
while (a <= 5) {
print(a)
a <- a + 1
3. For Loop:
Example: for (i in 1:5) {
print(i)
Module-2
Q3:
subtraction <- A - B
b. Lists in R:
1. Create a List: my_list <- list(name = "John", age = 25, scores = c(90, 85, 88))
a. Outputs of R Statements:
4. basename("C:/Program Files/test.R"):
Output: [1] "test.R"
3. Subset by Category:
CopyEdit
Module-3
Q5:
1. with():
Executes code within a data frame without explicitly referencing it.
Example: df <- data.frame(x = 1:3, y = c(4, 5, 6))
2. within():
Modifies a data frame by directly editing its columns.
Example:
print(df)
Output:
xy z
114 4
2 2 5 10
3 3 6 18
3. order():
Orders a data frame by specified columns.
Example:
df[order(df$x), ]
Output:
xy
214
325
136
ID = 1:10,
Salary = c(20000, 30000, 25000, 40000, 35000, 22000, 27000, 32000, 28000, 31000),
Department = c("IT", "HR", "Finance", "IT", "Finance", "HR", "IT", "Finance", "HR", "IT")
dim(emp)
max(emp$Salary)
emp[which.max(emp$Salary), ]
lapply(l, sum)
write.csv(data, "output.csv")
library(readxl)
data <- read_excel("file.xlsx")
library(writexl)
write_xlsx(data, "output.xlsx")
Module-4
Q7:
1. Basic Syntax:
2. Parameters:
3. Example:
1. Base R:
2. ggplot2:
library(ggplot2)
ggplot(data, aes(x = var1, y = var2)) + geom_point()
OR Q8:
Example:
b. Graphing functions:
1. hist():
2. plot():
3. boxplot():
library(lattice)
5. ggplot():
library(ggplot2)
Q9:
mean(x)
median(x)
sd(x)
var(x)
b. Normal Distribution in R:
1. Functions:
2. Example:
hist(x)
OR Q10:
plot(mtcars$hp, mtcars$mpg)
cor(mtcars$hp, mtcars$disp)
plot(mtcars$hp, mtcars$disp)
cor(mtcars)
1. Example:
summary(model)