R-programming Qp-2024
Ananya Institute of Commerce and Management
R-programming-Annual exam Paper-2024
Two marks questions
[Link] is R-programming? who developed it?
R is an open-source ,platform independent programming language and software
environment specifically designed for statistical computing and graphics. R was
developed by Ross Ihaka and Robert Gentleman
[Link] the difference between vector and List.
A vector is a collection of similar elements.c() function is used to create list
Ex: numbers<- c(1, 2, 3, 4, 5)
Lists is a collection of elements of different data types. This includes numbers,
[Link] is created by using list function.
Ex- my_list <- list(1, "hello", TRUE, c(5, 6))
[Link] any two applications of R-programming.
Applications are
➢ Statistical Analysis and Data Visualization:
➢ Organizing data
➢ Data Exploration and Cleaning.
[Link] NA and NAN with an example.
1. NA stands for Not available,it signifies a missing or unavailable value in a
dataset.
Ex- x <- c(1, 2, NA, 4)
print(x)
[Link]
NAN stands for Not a number,NaN represents the result of an undefined numerical
operation, such as dividing zero by zero or taking the square root of a negative
number.
Ex- y <- 4 / 0
Dept of BCA,AICM Page 1
R-programming Qp-2024
[Link] the mean,mode and median for the following numbers
12,13,11,10,9,11,7,11,10,15,16,11(total 12 numbers)
★ mean=(12+13+11+10+9+11+7+11+10+15+16+11)/12=136/12=11.333
★ For median arrange in sorted order 7,9,10,10,11,11,11,11,12,13,15,16
Median pos=(n+1)/2=(12+1)/2=13/2=6.5 i.e 6th and 7th element
So median is (11+11)/2=22/2=11
★ Mode is the number repeated highest number of times so mode is 11
[Link] is T-Distribution or Student’s T-distribution
The T-distribution (or Student's t-distribution) is a probability distribution used in
statistics when the sample size is small and the population's standard deviation is
unknown.
Student's t Distribution is used when :
● The sample size is 30 or less than 30.
● The population standard deviation(σ) is unknown.
● The population distribution must be unimodal and skewed.
[Link] is Anova-Test?
Anova-(Analysis of variance) is an analysis tool used to compare mean between
two or more items. It is a statistical method that yields values that can be tested to
determine whether a significant relation exits between variables.
[Link] is the difference between Histogram and Bar graph.
Histogram Bar graph
1. Histogram are used to visualize data [Link] graph are used to visualize data
by bars representing the frequency of by bars to compare different categories
numerical data. of data.
[Link] is no gap between adjacent [Link] is a gap between adjacent bars
bars.
[Link] width may or may not be same [Link] width is same
[Link] change the order of bars [Link] change the order of bars
Dept of BCA,AICM Page 2
R-programming Qp-2024
Five marks questions
[Link] different types of operators in R.
Different types of operators are
Dept of BCA,AICM Page 3
R-programming Qp-2024
Ex for Logical operators
Dept of BCA,AICM Page 4
R-programming Qp-2024
Ex for Assignment operators
[Link] is data frame?Explain any two operations on data frame.
DataFrames are used to store the tabular [Link] each column of a can be of
the different data types.
To create a data frame we use the [Link]() function in R .
Ex-Products <- [Link](
Product_ID = c(101, 102, 103),
Product_Name = c("T-Shirt", "Jeans", "Shoes"),
Price = c(15.99, 29.99, 49.99),
Stock = c(50, 30, 25)
)
Dept of BCA,AICM Page 5
R-programming Qp-2024
Operations on data frame.
[Link] new row to data frame by using rbind() function
New_Product <- c(104, "Sunglasses", 39.99, 40)
Products <- rbind(Products, New_Product)
[Link] new column to data frame by using cbind() function
Discount <- c(5, 10, 8)
Products <- cbind(Products, Discount)
Product_ID Product_Name Price Stock Discount
101 T-Shirt 15.99 50 5
102 Jeans 29.99 30 10
103 Shoes 49.99 25 8
104 Sunglasses 39.99 40 7
[Link] different matrix operations function in R.
Matrix in R are table like structure with similar data-type arranged by taking number
of rows and columns.
matrix() is used to create a matrix.
Operations on Matrices
a)Matrix Addition
Matrix addition is the process of adding corresponding elements from two matrices
Ex- A <- matrix(c(1, 2 , 4, 3, 4, 5, 2, 3))
B <- matrix(c(2, 3, 6, 3, 4, 5, 2, 3))
print(A + B)
b)Matrices Subtraction
It involves subtracting corresponding elements from the first matrix by the second
matrix.
Ex- A <- matrix(c(5, 4 , 7, 3, 5, 8, 9, 4))
B <- matrix(c(2, 3, 6, 3, 4, 5, 2, 3))
print(A - B)
Dept of BCA,AICM Page 6
R-programming Qp-2024
c)Matrix Multiplication
Here, each element in one matrix is multiplied by the corresponding element in the
second matrix.
Ex- A <- matrix(c(1, 2, 3, 4), nrow = 2)
B <- matrix(c(5, 6, 7, 8), nrow = 2)
result <- A * B
print(result)
[Link] a r code to find the factorial of a given number using recursion
factorial <- function(n)
{
if(n==0)
return (1)
else
return (n * factorial (n-1))
}
n=[Link](readLine("enter a number"))
factorial(n)
[Link] is probability?Explain Probability mass function.
Probability denotes the possibility of the outcome of any random event.
➢ Example-Probability of flipping a coin:There are two possible outcomes—
heads or tails.
➢ It is a scale from 0 to 1, where 0 means the event is impossible and 1 means
it is certain.
➢ For example, the probability of rolling a 7 on a standard six-sided die is 0,
while the probability of rolling any number from 1 to 6 is
Probability mass function.
A probability mass function (PMF) is a function used for discrete random variables
that gives the probability of the variable being exactly equal to a certain value.
f(x)=P(X=x)
Dept of BCA,AICM Page 7
R-programming Qp-2024
Figure:Probability mass
functions(pmf)
[Link] a r code for calculating cumulative sums and maxima, minima and
calculus.
x <- c(3, 1, 4, 1, 5, 9)
cum_sum <- cumsum(x)
print(cum_sum)
cum_max <- cummax(x)
print(cum_max)
cum_min <- cummin(x)
print(cum_min)
derivative <- c(NA, diff(x))
print(derivative)
Dept of BCA,AICM Page 8
R-programming Qp-2024
Seven marks questions
[Link] different data structures in R.
Data structures in R programming provide a way to organize and store data
efficiently.
Different data structures in R are:
● Vectors
● Lists
● Dataframes
● Matrices
● Arrays
● Factors
a)Vector
A vector is a fundamental data structure that holds a sequence of elements of the
same data type. It is a one-dimensional array-like object that can store values such
as numbers, characters etc.
Example
my_vector1 <- c(1, 2, 3, 4, 5)
my_vector2 <- c("apple", "banana", "orange")
print(my-vector1)
print(my-vector2)
b)Lists
Lists can store elements of different data types and even other data structures (like
vectors, matrices, or other lists).
Example:
my_list <- list(name = "Alice", age = 30, scores = c(85, 92, 78))
print(my_list)
c)Dataframe
In R, a dataframe is a two-dimensional data structure that organizes data into rows
and columns, similar to a table
Dept of BCA,AICM Page 9
R-programming Qp-2024
Ex-
Products <- [Link](
Product_ID = c(101, 102, 103),
Product_Name = c("T-Shirt", "Jeans", "Shoes"),
Price = c(15.99, 29.99, 49.99),
Stock = c(50, 30, 25)
)
Output
Product_ID Product_Name Price Stock
101 T-Shirt 15.99 50
102 Jeans 29.99 30
103 Shoes 49.99 25
d)Matrices
Matrix in R are table like structure with similar data-type arranged by taking number
of rows and columns.
matrix() function iis used to create a matrix.
Example
my_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)
print(my_matrix)
e)Arrays
Arrays are used to store homogenous data with contiguous memory [Link]
have fixed number of dimensions.
Array is created by using array() function
Example
my-array<- array(c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120), dim = c(2, 3,
2))
print(my_array)
f)Factors
A factor stores categorical data with levels.
Example
transport <- factor(c("Bus", "Car", "Bike", "Car", "Bus", "Bike", "Car"))
transport
Output
Bus Car Bike Car Bus Bike Car
Levels: Bike Bus Car
Dept of BCA,AICM Page 10
R-programming Qp-2024
[Link] control structures in R with example
Control statements are expressions used to control the execution and flow of the
program based on the conditions provided in the statements.
Types of control statements in R:
a)if condition
if condition
Checks if the expression is true. If true, executes the statements inside { }.
Example
x <- 100
if(x > 10)
print(paste(x, "is greater than 10"))
b)if-else condition
Executes statements in if block if condition is TRUE; else block if FALSE.
Example:
x <- 5
if(x > 10){
print(paste(x, "is greater than 10"))
}else{
print(paste(x, "is less than 10"))
c)Nested if condition
Used to check multiple conditions by nesting one if inside another.
Dept of BCA,AICM Page 11
R-programming Qp-2024
Example:
x <- 15
if(x > 10){
if(x %% 2 == 0){
print("x is greater than 10 and even")
}else{
print("x is greater than 10 and odd")
}else{
print("x is 10 or less")
d)Else if condition
Used to check multiple mutually exclusive conditions sequentially.
Example:
x <- -5
if(x > 0){
print("Positive number")
}else if(x < 0){
print("Negative number")
}else{
print("Zero")
Dept of BCA,AICM Page 12
R-programming Qp-2024
e)for loop
It is the most widely used loop. It Executes a block repeatedly until all elements in a
sequence are processed.
Example”
for(i in 1:10)
print(i)
f)while loop
It is a entry controlled statement where condition is first checked and executes
statements while the condition is TRUE.
Example:
x=1
while(x <= 5)
print(x)
x=x+1
g)repeat loop and break statement
repeat executes indefinitely until break is used.
Example:
x=1
repeat
Dept of BCA,AICM Page 13
R-programming Qp-2024
print(x)
x=x+1
if(x > 5)
break
18..Explain different functions for statistical distribution.
Dept of BCA,AICM Page 14
R-programming Qp-2024
Dept of BCA,AICM Page 15
R-programming Qp-2024
[Link] is box plot?Explain the importance of box plot with an example.
A box plot (also called a box-and-whisker plot) in R is a graphical representation of
the distribution of numerical data. It summarizes data using five key values:
1. Minimum
2. First Quartile (Q1)
3. Median (Q2)
4. Third Quartile (Q3)
5. Maximum
➢ Box plot is a visualisation tool to identify the presence of outliers if any in the
given data.
Dept of BCA,AICM Page 16
R-programming Qp-2024
➢ Any values greater than the upper limit or less than lower limit is an outlier.
Figure :Box plot with Qualifiers(Q1,Q2,Q3)lower upper limits &
outliers
Ex- marks <- c(45, 56, 67, 78, 88, 92, 95, 40, 38, 100, 98)
boxplot(marks,
main = "Box Plot of Student Marks",
ylab = "Marks")
Output
Dept of BCA,AICM Page 17
R-programming Qp-2024
[Link] a program to create an application of Linear Regression in
multivariate context for predictive purpose.(Lab program 9)
num_samples <- 100
square_footage <- runif(num_samples, min=800, max=3000)
num_bedrooms <- sample(2:5, num_samples, replace=TRUE)
num_bathrooms <- sample(1:3, num_samples,replace=TRUE)
house_prices <- 50000 + 250 * square_footage + 15000 * num_bedrooms + 10000 *
num_bathrooms + rnorm(num_samples, mean=0, sd=20000)
# Creating a data frame with the sample data
data<-[Link](SquareFootage=square_footage,Bedrooms=num_bedrooms,
Bathrooms=num_bathrooms, Price=house_prices)
# Perform multivariate linear regression
model<-lm(Price ~ SquareFootage + Bedrooms + Bathrooms, data=data)
# Summary of the regression model
print(summary(model))
Dept of BCA,AICM Page 18