tough R Programming questions and answers focuses on “Commands”.
1. Which of the following will reverse the order of values in x?
a) rev(x)
b) max(x, na.rm=TRUE)
c) all(x)
d) x%in%y
Answer: a
Clarification: rev provides a reversed version of its argument.
2. Which of the following finds row sums for each level of a grouping variable?
a) rowSums (x, na.rm = FALSE, dims = 1)
b) colMeans(x, na.rm = FALSE, dims = 1)
c) rowMeans(x, na.rm = FALSE, dims = 1)
d) rowsum(x, group, reorder = TRUE, …)
Answer: c
Clarification: False value leads to unexpected result.
3. Which of the following statement applies the function (FUN) to either rows (1) or columns (2) on object X?
a) apply(x,1,min)
b) apply(x,2,max)
c) col.max(x)
d) apply(X, MARGIN, FUN, …)
Answer: d
Clarification: apply(x,2,max) finds the maximum for each column.
4. Which of the following statement finds the maximum for each column?
a) apply(x,2,max)
b) col.max(x)
c) which.min(x)
d) which.max(x)
Answer: a
Clarification: col.max(x) is another way to find which column has the maximum value for each row.
5. which of the following statement tells the row with the minimum value for every column?
a) which.min(x)
b) which.max(x)
c) z=apply(x,1,which.min)
d) z=apply(1,1,which.max)
Answer: a
Clarification: which() function gives the TRUE indices of a logical object, allowing for array indices.
6. Which of the following may be used for linear regression?
a) X %*% Y
b) solve(A)
c) solve(A,B)
d) X $*$ Y
Answer: c
Clarification: solve(A,B) implies inverse of A * B.
7. Which of the following finds row sums for each level of a grouping variable?
a) as.numeric(x)
b) rowsum(x, group, reorder = TRUE, …)
c) as.order(x)
d) colSums (x, na.rm = FALSE, dims = 1)
Answer: b
Clarification: rowsum compute column sums across rows of a numeric matrix-like object for each level of a grouping variable.
8. Which of the following sets the size of the outer margins for the graph?
a) par(mfrow=c(nrow,mcol))
b) par(ask=TRUE)
c) par(omi=c(0,0,1,0) )
d) par(ask=False)
Answer: c
Clarification: par can be used to set or query graphical parameters.
9. Which of the following function is used for plotting histogram?
a) hist()
b) histog()
c) histg()
d) histo()
Answer: a
Clarification: The generic function hist computes a histogram of the given data values.
10. Which of the following will add the title “R language” to the graph?
a) titleAdd( “R language”)
b) title( “R language”)
c) titleBar( “R language”)
d) var(x, na.rm=TRUE)
Answer: b
Clarification: This function can be used to add labels to a plot.
