R - SEC - 2022 - Solution DU CBCS
R - SEC - 2022 - Solution DU CBCS
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)
d) For the given factor f<-factor(c("abc", "abc", "cab", "bac", "abc", "cab", 2
"cab")), what will table(f) return?
3.
a) Give the output of the following command: 2
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:
package.skeleton(
"my_area",
c("rect_area", "rect"))
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
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")
or
or
read.csv(“my_file.txt”, header = TRUE, sep = "\n", na.strings="", quote
= ‘ ‘)
f <- function(x)
{
f <- function(x)
{
print(x^2)
}
f(x) + 1
}
f(5) #statement 1
[1] 25
[1] 26