Notes Chp5 R Programming
Notes Chp5 R Programming
Exercises
answer: c
2. How many types of R objects are present in R data type?
a) 4
b) 5
c) 6
d) 7
answer: c
3. In vector, where all arguments are forced to a common type, please tick the correct
order:
a) Expression <logical < double
b) Double < logical <NULL
c) Integer < character < expression
d) Character < logical < raw
answer: c
4. Which function is used to create the vector with more than one element?
a) Library ()
b) plot ()
c) c ()
d) par ()
answer: c
5. Which of the following is incorrect regarding List in R programming?
a) A list is a type of R object containing elements of different types like numbers, strings,
vectors.
b) A list can also contain another list within it
c) Elements in a list can be accessed using the index of the element in the list.
d) The addition or deletion of the elements can only be done at the middle of the list.
Answer: d
6. A _________ is a two-dimensional rectangular data set.
a) Vector
b) Lists
c) Matrix
d) Functions
Answer: c
7. Which function takes a dim attribute which creates the required number of
dimensions?
a) Vector
b) Array
c) Matrix
d) Lists
Answer: b
8. By what function we can create data frames?
a) Data.frames ()
b) Data.sets ()
c) Function ()
d) C ()
Answer: a
9. Which are indexed by either row or column using a specific name or number?
a) Datasets
b) Data frames
c) Data
d) Functions
Answer: b
10. The geom function used to plot scatter plot while using ggplot2 package is:
a) geom_point()
b) geom_boxplot()
c) geom_line()
d) None of the above
Answer: a
STANDARD QUESTIONS:
1. Write a program in R to create a multi-element vector having the first ten natural numbers and print it.
Answer:
v1<-c(1:10)
print(v1)
2. Suppose we have two vectors, V1 = [2, 3, 0, 5] and V2 = [6, 1, 7, 0]. What will be the result of the
following?
a) V1+V2
[8, 4, 7, 5]
b) V1- V2
[-4, 2, -7, 5]
c) V1*V3
[12, 3, 0, 0]
d) V1/V2
[0.33, 3, 0, Inf]
3. Write an R program to create two numeric matrices and perform arithmetic operations like addition,
subtraction, multiplication, and division. (refer to the chapter for pointers)
m1<-matrix(c(1:9),nrow=3, byrow=TRUE)
m2<-matrix(c(2:10),nrow=3, byrow=TRUE)
sum<-m1+m2
sub<-m2-m1
prod<-m2*m1
div<-m2/m1
sum
sub
prod
div
4. Draw a bar plot in R, showing the number of days (y-axis) each month for 2016(x-axis).
months <- c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December")
days <- c(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
barplot(height = days,
names.arg = months,
col = "skyblue",
main = "Number of Days in Each Month (2016)",
xlab = "Months",
ylab = "Number of Days",
las = 2, # Rotate x-axis labels
cex.names = 0.8)
5. The table below shows the number of water filters sold in the first six months of the year 2019 by two
different stores, Store A & Store B.
Month Jan Fe Mar Apr May Jun
b
Store 32 34 35 28 37 45
A
Store 28 22 34 36 25 20
B
Use the data shown in the table above to draw a side by side stacked chart, having Month in X axis,
Number of water filters sold in Y axis and legend for bars of Store A & Store B.
Month<-c("Jan","Feb","Mar","Apr","May","Jun")
StoreA<-c(32,34,35,28,37,45)
StoreB<-c(28,22,34,36,25,20)
regions<-c("StoreA","StoreB")
dt<-matrix(c(StoreA,StoreB),nrow=2,byrow=TRUE)
barplot(dt,names.arg=Month,col=c("red","blue"),
main="Water Filters",xlab="Month",beside=TRUE)
legend("topright",regions,cex=0.6,fill=c("red","blue"))
6. Draw a pie chart in R to represent days in each month for the year 2016.
months <- c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December")
days <- c(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
pie(days,months, main="Months in 2016",col=rainbow(length(months)))
7. For a given vector (7, 3, 8, 6, 5, 7, 1, 9, 2, 7, 6), write a program in R to calculate and display the
mean, median, and mode of the vector.
dt<-c(7, 3, 8, 6, 5, 7, 1, 9, 2, 7, 6)
mean(dt)
median(dt)
mode<-function(d){
unq<-unique(d)
pos<-match(d,unq)
t<-tabulate(pos)
mx<-which.max(t)
unq[mx]
}
res<-mode(dt)
print(res)
Using geom_point()
library(ggplot2)
p<-ggplot(Orange, aes(x = age, y = circumference))
p+geom_point()
2. In R, we have an inbuilt dataset named Orange. Plot a boxplot showing age(xaxis) vs
circumference(y-axis). The boxplot should be plotted first using the normal function in R and then
using the ggplot2 library.
boxplot(circumference ~ age,
data = Orange,
main = "Boxplot of Circumference by Age",
xlab = "Age (days)",
ylab = "Circumference (mm)",
col = "lightblue",
notch = FALSE)
# ggplot2 boxplot
library(ggplot2)
ggplot(data = Orange, aes(x = factor(age), y = circumference)) +
geom_boxplot(fill = "lightblue", notch = FALSE) +
labs(
title = "Boxplot of Circumference by Age",
x = "Age (days)",
y = "Circumference (mm)"
)
3. In R, we have an inbuilt data set named Orange. Plot a histogram for the circumference of the Orange
dataset.
# Base R histogram
hist(Orange$circumference,
main = "Histogram of Circumference",
xlab = "Circumference (mm)",
col = "lightgreen",
border = "black",
breaks = 10)
4. In R, we have an inbuilt data set named BOD. Use the data set to draw a line chart plotting Time vs.
Demand.
# Base R line chart
plot(BOD$Time, BOD$demand,
type = "o",
col = "blue",
main = "Time vs. Demand",
xlab = "Time (days)",
ylab = "Demand (mg/L)")