0% found this document useful (0 votes)
18 views6 pages

R - SEC - 2022 - Solution DU CBCS

The document provides solutions to various R programming questions, including variable assignments, data frame manipulations, and database interactions using the RMySQL package. It covers functions for extracting data, performing calculations, and generating plots, along with explanations of R syntax and expected outputs. Additionally, it includes tasks related to reading files and defining functions in R.

Uploaded by

Saheba Kapoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

R - SEC - 2022 - Solution DU CBCS

The document provides solutions to various R programming questions, including variable assignments, data frame manipulations, and database interactions using the RMySQL package. It covers functions for extracting data, performing calculations, and generating plots, along with explanations of R syntax and expected outputs. Additionally, it includes tasks related to reading files and defining functions in R.

Uploaded by

Saheba Kapoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

B.

Sc(H) Computer Science-IV Sem SEC- Introduction to R Programming Solution


Part A

1. Answer the following questions:

a) What value will be stored in variable “X”? 1


X <- vector("complex", 3)

[1] 0+0i 0+0i 0+0i

b) Give an R statement to extract the rows from a data frame “df” that does 1
not have missing values.

complete.cases(df) or na.omit(df)

c) Given the output at statement 1 and 2 in the following R script. 2


y <- c(2, 1, 5, 7, 8, 3, 2, 4, 5)
length(y) <- 4
print(y) #statement 1 [1] 2 1 5 7
length(y) <- 6
print(y) #statement 2 [1] 2 1 5 7 NA NA

d) For the given factor f<-factor(c("abc", "abc", "cab", "bac", "abc", "cab", 2
"cab")), what will table(f) return?

abc bac cab


3 1 3

e) What are the two compulsory files in a package directory structure? 2


1. DESCRIPTION contains details about the package’s version, its
author, and what it does.
2. NAMESPACE describes which functions will be exported.
f) What is the difference between the functions “read.csv” and 2
“read.csv2”?
1. read.csv is used for data where commas are used as separators
while read. csv2 is for data where semicolons are used as
separators.
2. read.csv is used for data where periods are used as decimals,
while read. csv2 is for data where commas are used as decimals.
Part B
2. Consider a data frame: 5

Student(roll_no, name, city, course)

Write the name of in-built R functions of MySQL package to perform


the following tasks:

i) Load relevant packages


library(RMySQL)
library(DBI)

ii) Establish the connection with the database MySQL.


con <- dbConnect(dbDriver(“MySQL”), “path/to/MySQL/
db1”)

iii) Display all tables in the database.


dbListTables(con)

iv) Return the total number of students in the “Student” table in


the database.
dbSendQuery(con, "SELECT count(*) FROM Student)
or
dbGetQuery(con, "SELECT count(*) FROM Student)

v) Close the database connection.


dbDisconnect(con)

3.
a) Give the output of the following command: 2

switch (5%/%2, sum(2:8), summary(c("a", "b")), sample(10, 5))

Length Class Mode


2 character character

b) Given a list L as: 3


L <- list(
a = 2,
b = 3,
twin = c(2, 2),
trip = c(2, 2, 2)
)
What will be the output of following R statements?
i) unlist(L)
a b twin1 twin2 trip1 trip2 trip3
2 3 2 2 2 2 2
ii) lapply(L, length)
$a
[1] 1
$b
[1] 1
$twin
[1] 2
$trip
[1] 3

iii) sapply(L, length)


a b twin trip
1 1 2 3
4 Consider the following data frame “df”. 1+2+2

SNo Value Class


1 98 A
2 21 B
3 67 C
4 23 A
5 11 A
6 12 C
7 34 C
8 56 B
9 78 A
10 90 C
11 12 C

Write an R script to perform the following:

a. Display the rows of “df” where Class is “A”.


df[df$Class=="A",]

b. Display total number of values in each class. table(df$Class)


c. Plot a boxplot to show the statistical summary of all values with
respect to their class.
boxplot(df[, 2] ~ df[, 3]) or boxplot(df$Value ~ df$Class)

5 a) Give a data frame “rect” containing the length and breadth of five 2
rectangles and a function “rect_area” to compute the area of rectangles
as:

rect <- data.frame(L=c(10, 5.5, 6, 7.8, 9.7), B=c(6, 4, 1.2, 3, 4))

rect_area <- function(a, b)


{
a*b
}

Write an R statement to create a package called “my_area” to compute


the area of rectangles using given data frame and function.

package.skeleton(
"my_area",
c("rect_area", "rect"))

c) For the given vectors “x” and “y”, 3


x <- matrix(rep(1:3, each =2), nrow=3 , ncol=2)
y<- matrix(rep(1:3, length.out=6), nrow=2, ncol=3)
What will be the output of:

i) x%*% y
[,1] [,2] [,3]
[1,] 5 5 8
[2,] 7 6 11
[3,] 8 9 13

ii) x*t(y)
[,1] [,2]
[1,] 1 4
[2,] 3 3
[3,] 4 9
6 Consider the following dataset that shows the number of times the 5
tasks are performed by either wife, husband, and jointly by P1 and P2.:

Task\Person P1 P2 Jointly
Laundry 56 34 4
Meal 24 10 4
Cleaning 53 23 20
Dishes 32 56 40
Finances 13 23 70
Driving 10 78 0
Holidays 0 4 0

Write an R script to:

i) Find the tasks which were more performed by P1 than P2.


df[df$P1>df$P2, 1] (1)

ii) Find the total number of tasks performed jointly by P1 and P2.
df[df$J>0, 1]) (1)

iii) Give a suitable plot to show the frequency of each task performed
by P1 and P2. Give appropriate labels and legends. (3)
barplot(t(df[,c(1,2)]),beside=TRUE,col=c("blue","red"), xlab =
"Tasks", ylab="Frequency", main="Freq of tasks by P1 and P2")

legend("topright", legend=c("P1","P2"), col=c("blue","red"),


pch=2)

(any of the locations of legend, any value of pch, any 2 colors)

or

barplot(df$P1, xlab="Tasks",ylab = "Freq", main="Freq of Tasks by


P1")
> barplot(df$P2, xlab="Tasks",ylab = "Freq", main="Freq of Tasks
by P2")

7 a) Give an R script to read a file “my_file.txt”: 2


i) headers as in input file,
ii) separator as new line character,
iii) indicate blank rows as missing values.
iv) quoting strings as ‘ ‘

read.delim(“my_file.txt”, header = TRUE, sep = "\n", na.strings="",


quote = ‘ ‘)
or

read.table(“my_file.txt”, header = TRUE, sep = "\n", na.strings="",


quote = ‘ ‘)

or
read.csv(“my_file.txt”, header = TRUE, sep = "\n", na.strings="", quote
= ‘ ‘)

b) Give the output at statement (1) when following R script is executed: 3

f <- function(x)
{
f <- function(x)
{
print(x^2)
}
f(x) + 1
}

f(5) #statement 1

[1] 25
[1] 26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy