1. Which of the following code opens a connection to the file foo.txt, reads from it, and closes the connection when its done?
a) data < - read.csv(“foo.txt”)
b) data < - read.csvo(“foo.txt”)
c) data < - readonly.csv(“foo.txt”)
d) data < - getonly.csv(“foo.txt”)
Explanation: Connections must be opened, then the are read from or written to, and then they are closed.
2. Which of the following opens connection to gz-compressed text file?
a) con < - gzfiles(“words.gz”)
b) con < - gzfile(“words.gz”)
c) con < - gzfile2(“words.gz”)
d) con < - gzfiles2(“words.gz”)
Explanation: For more structured text data like CSV files or tab-delimited files, there are other functions like read.csv() or read.table().
3. Which of the following extracts first element from the following R vector?
> x < - c("a", "b", "c", "c", "d", "a")
a) x[10]
b) x[1]
c) x[0]
d) x[2]
Explanation: The element which we want to extract will be in the format of variable[index value of the element] in R script.
4. Point out the correct statement?
a) There are three operators that can be used to extract subsets of R objects
b) The [ operator is used to extract elements of a list or data frame by literal name
c) The [[ operator is used to extract elements of a list or data frame by string name
d) There are five operators that can be used to extract subsets of R objects
Explanation: Three operators are [,[[ and $.
5. Which of the following extracts first four element from the following R vector?
> x < - c("a", "b", "c", "c", "d", "a")
a) x[0:4]
b) x[1:4]
c) x[0:3]
d) x[4:3]
Explanation: The multiple successive elements which we want to extract will be in the format of variable[index value of the start element:index value of the last element] in R script.
6. What will be the output of the following R code?
> x < - c("a", "b", "c", "c", "d", "a")
> x[c(1, 3, 4)]
a) “a” “b” “c”
b) “a” “c” “c”
c) “a” “c” “b”
d) “b” “c” “b”
Explanation: The sequence does not have to be in order; you can specify any arbitrary integer vector.
7. Point out the wrong statement?
a) $ operator semantics are similar to that of [[
b) The [ operator always returns an object of the same class as the original
c) The $ operator is used to extract elements of a list or a data frame
d) There are three operators that can be used to extract subsets of R objects
Explanation: The [[ operator is used to extract elements of a list or a data frame. It can only be used to extract a single element and the class of the returned object will not necessarily be a list or data frame.
8. What will be the output of the following R code?
> x < - matrix(1:6, 2, 3)
> x[1, 2]
a) 3
b) 2
c) 1
d) 0
Explanation: Matrices can be subsetted in the usual way with (i,j) type indices where i is the row and j is the column numbers.
9. What will be the output of the following R code?
> x < - matrix(1:6, 2, 3)
> x[1, ].
a) 1 3 5
b) 2 3 5
c) 3 3 5
d) file
Explanation: Matrices can be subsetted in the usual way with (i,j) type indices where i is the row and j is the column numbers. If only row or only column number is specified, then the respective full row or column is printed.
10. Which of the following R code extracts the second column for the following matrix?
> x < - matrix(1:6, 2, 3)
a) x[2, ]
b) x[1, 2]
c) x[, 2]
d) x[1 1 2]
Explanation: This behavior is used to access entire rows or columns of a matrix.