R Programming Questions and Answers Part-15

1. Which of the following function is identical to read .table?
a) read.csv
b) read.data
c) read.tab
d) read.del

Answer: a
Explanation: The read.csv() function is identical to read.table except that some of the defaults are set differently (like the sep argument).

2. Which of the following code would read 100 rows?
a) initial < - read.table(“datatable.txt”, nrows = 100)
b) tabAll < - read.table(“datatable.txt”, colClasses = classes)
c) initial < - read.table(“datatable.txt”, nrows = 99)
d) initial < - read.table(“datatable.txt”, nrows = 101)

Answer: a
Explanation: You can use the Unix tool wc to calculate the number of lines in a file.

3. Individual R objects can be saved to a file using the _____ function.
a) save
b) put
c) save_image
d) get

Answer: a
Explanation: The key functions for converting R objects into a binary format are save(), save.image(), and serialize().

4. Point out the correct statement?
a) The complement to the textual format is the binary format
b) If you have a lot of objects that you want to save to a file, you can save all objects in your workspace using the save.image() function
c) The serialize() function is used to convert individual R objects into a binary format that can be communicated across an arbitrary connection
d) All of the mentioned

Answer: d
Explanation: It’s better to stick with a binary format for efficiency and accuracy.

5. Which of the following R statement will save the output to the file for following R code?
> a < - data.frame(x = rnorm(100), y = runif(100))
> b < - c(3, 4.4, 1 / 3)
a) save(a, b, file = “mydata.rda”)
b) save_image(a, b, file = “mydata.rda”)
c) keep(a, b, file = “mydata.rda”)
d) keep_image(a, b, file = “mydata.rda”)

Answer: a
Explanation: You can save all objects in your workspace using the save.image() function.

6. Which of the following statement will load the objects to the file named “mydata.RData”?
a) save(“mydata.RData”)
b) load(“mydata.RData”)
c) loadAll(“mydata.RData”)
d) put(“mydata.RData”)

Answer: b
Explanation: rda and .RData are fairly common extensions and you may want to use them because they are recognized by other software.

7. Point out the wrong statement?
a) When you call unserialize() on an R object, the output will be a raw vector coded in hexadecimal format
b) serialize() function is the only way to perfectly represent an R object in an exportable format
c) .rda extension is used when save() function is incorporated
d) The complement to the textual format is the binary format

Answer: a
Explanation: Output may get sent to a file, but it could get sent over a network or other connection.

8. ________ opens a connection to a file compressed with gzip.
a) url
b) gzfile
c) bzfile
d) file

Answer: b
Explanation: “file” opens a connection to a file.

9. Connections to text files can be created with the ________ function.
a) url
b) gzfile
c) bzfile
d) file

Answer: d
Explanation: The file() function has a number of arguments that are common to many other connection functions.

10. Which of the following R code creates a connection to ‘foo.txt’?
a) con < - file(“foo.txt”)
b) open(con, “r”)
c) opencon(con, “r”)
d) ocon(con, “r”)

Answer: a
Explanation: Open is used for opening connection to ‘foo.txt’ in read-only mode.