R Programming Questions and Answers Part-14

1. Which of the following is invalid assignment?
a) > x < - list("Los Angeles" = 1, Boston = 2, London = 3)
b) > names(x) < - c("New York", "Seattle", "Los Angeles")
c) > name(x) < - c("New York", "Seattle", "Los Angeles")
d) > names(x) < - c("New York", "Los Angeles", "Los Angeles")

Answer: c
Explanation: Lists can also have names, which is often very useful.

2. What will be the output of the following R code?
> x < - 1:3
> names(x)
a) NULL
b) 1
c) 2
d) 4.5

Answer: a
Explanation: R objects can have names, which is very useful for writing readable code and self-describing objects.

3. Which of the following statement changes column name to h and f?
a) colnames(m) < - c(“h”, “f”)
b) columnnames(m) < - c(“h”, “f”)
c) rownames(m) < - c(“h”, “f”)
d) rownames(m) < - c(“f”, “f”)

Answer: a
Explanation: Column names and row names can be set separately using the colnames() and rownames() functions.

4. Which of the following is used for reading tabular data?
a) read.csv
b) dget
c) readLines
d) writeline

Answer: a
Explanation: read.table can also be used for reading dataset in structured form.

5. Which of the following is used for reading in saved workspaces?
a) unserialize
b) load
c) get
d) set

Answer: b
Explanation: unserialize is used for reading single R objects in binary form. Load is used for reading in saved workspaces. Search by name for an object (get) or zero or more objects (mget).

6. Point out the wrong statement?
a) write.table is used for for writing tabular data to text files (i.e. CSV) or connections
b) writeLines is used for for writing character data line-by-line to a file or connection
c) dump is used for for dumping a textual representation of multiple R objects
d) all of the mentioned

Answer: d
Explanation: There are analogous functions for writing data to files.

7. ________ is used for outputting a textual representation of an R object.
a) dput
b) dump
c) dget
d) dset

Answer: a
Explanation: dump is used for dumping a textual representation of multiple R objects.

8. Which of the following argument denotes if the file has a header line?
a) header
b) sep
c) file
d) footer

Answer: a
Explanation: sep is a string indicating how the columns are separated.

9. Point out the correct statement?
a) unserialize is used for converting an R object into a binary format for outputting to a connection
b) save is used for saving an arbitrary number of R objects in binary format to a file
c) The read.data() function is one of the most commonly used functions for reading data
d) save is not used for saving an arbitrary

Answer: b
Explanation: read.table reads a file in table format and creates a data frame from it.

10. Which of the following statement would read file “foo.txt”?
a) data < - read.table(“foo.txt”)
b) read.data < - read.table(“foo.txt”)
c) data < - read.data(“foo.txt”)
d) data < - data(“foo.txt”)

Answer: a
Explanation: R will automatically skip lines that begin with a #.