MOOC Course ‐ Introduction to R Software
July 2017
Solution to Assignment 5
1. The command print(1/3,digits=5) will print five significant digits as given by
digits=5.
Hence option c is correct.
2. The instructions inside the quotes " " are printed as such and the instructions without the
quotes "" are computed.
Hence option c is correct.
[Link] are three different print statements separated by semi colon. The output will be
executed in three different lines.
Hence option b is correct.
4. Please note the difference between print and the cat function. The print function
prints the entire thing. The cat function concatenates and prints the argument in strings.
Hence option d is correct.
5. The function paste()in R concatenates several strings together.
Hence option b is correct.
6. The function cat in R converts its arguments to character vector and combines them
into a continuous output.
Hence option d is correct.
7. The function cat in R converts its arguments to character vector and combines them into
a continuous output.
Hence option c is correct.
8. The function paste()in R concatenates several strings together. If we use the parameter
collapse, a single string instead of a vector of strings is created.
Hence option b is correct.
9. The function paste()in R concatenates several strings together. The strings are
separated by *.
Hence option d is correct.
10. The function paste()in R concatenates several strings together. The strings are
separated by + sign. If we use the parameter collapse, a single string instead of a vector
of strings is created.
Hence option a is correct.
11. The command strsplit splits the elements of a character vector .
Hence option c is correct.
12. The command strsplit splits the elements of a character vector
Hence option c is correct.
13. The command nchar takes a character vector as an argument and returns a vector
whose elements contain the sizes of the corresponding elements of the given character
vector.
Hence option d is correct.
14. The syntax of the sub function in R is sub(old,new,string). The sub function
finds the first instance of the old substring and replaces it with the new substring. So in the
given string, 25 will be replaced by 30.
Hence option b is correct.
15. The syntax of the gsub function in R is gsub(old,new,string). The gsub function
finds all the instance of the old substring and replaces it with the new substring. So in the
given string, all R‐Course will be replaced by S‐plus Course.
Hence option a is correct.
16. The command tolower(x) converts upper case characters in a character vector to
lower case.
Hence option b is correct.
[Link] command toupper(x) converts lower case characters in a character vector to
upper case.
Hence option c is correct.
18. The syntax for grep function is grep(pattern,x). It searches for matches to
argument pattern within each element of a character vector x. By default, it prints the
location in terms of position.
Hence option c is correct.
19. The reason is the same as for solution of Q.18. It searches the position of the letters
a,b,c,d in the set of alphabets.
Hence option d is correct.
20. The command eval() evaluates but eval("") prints the string "".
Hence option a is correct.