R Programming Questions and Answers Part-3

1. What is the class defined in the following R code?
y<-c(FALSE,2)
a) Character
b) Numeric
c) Logical
d) Integer

Answer: b
Explanation: Numeric and FALSE is executed as 0. It is somewhat different from other programming languages. Console will give a class as Numeric. A vector can only contain objects of the same class. A list is represented as a vector but can contain objects of different classes.

2. Which one of the following is not a basic datatype?
a) Numeric
b) Character
c) Data frame
d) Integer

Answer: c
Explanation: Data frame is not the basic data type of R. Numeric, character, integer are the basic types of R. The basic data types are used many times. Data frames are used to store tabular data in R. They are an important type of object in R and are used in a variety of statistical modelling applications.

3. How do you create an integer suppose 5 in R?
a) 5L
b) 5l
c) 5i
d) 5d

Answer: a
Explanation: To create an integer L should be added to the integer. L is added to specify that it is an integer. An integer can also be created with many types. If you explicitly want an integer, you need to specify the L suffix.

4. What will be the output of the following R code?
x<- c (“a”,” b”)
as.numeric(x)
a) [1] 1 2
b) [1] TRUE TRUE
c) [1] NA NA (Warning message: NAs introduced by coercion)
d) [1] NAN

Answer: c
Explanation: Characters cannot be expressed as numeric. Therefore NA’s are printed as output. NA will specify the missing elements in the list. When nonsensical coercion takes place, you will usually get a warning from R.

5. The dimension attribute is itself an integer vector of length _______
a) 1
b) 2
c) 3
d) 4

Answer: b
Explanation: It is itself an integer vector of length 2. The dimension attribute in R is an integer vector. Real values larger in modulus than the largest integer are coerced to NA. Matrices are vectors with a dimension attribute. The dimension attribute is itself an integer vector of length 2 (number of rows, number of columns).

6. How could be the matrix constructed by using the following R code?
m <- matrix(1:6, nrow = 2, ncol = 3)
a) row-wise
b) column-wise
c) any manner
d) data insufficient

Answer: b
Explanation: If nothing is mentioned, matrix is created column-wise. If we want in row-wise then we have to specify. We have to mention “by row” to create a matrix in row wise. The filter( ) function is used to extract subsets of rows from a data frame. This function is similar to the existing subset( ) function.

7. Matrices can be created by row-binding with the help of the following function.
a) rjoin()
b) rbind()
c) rowbind()
d) rbinding()

Answer: b
Explanation: rbind() is used to create a matrix by row-binding. Row- binding is the basic function of R. R – bind is used to bind the functions in R. Matrices can be created by column-binding or row-binding with the cbind() and rbind() functions.

8. What is the function used to test objects (returns a logical operator) if they are NA?
a) is.na()
b) is.nan()
c) as.na()
d) as.nan()

Answer: a
Explanation: is.na() is the function used to test if they are NA. We can check NA ‘s at any stage of the code. Generally, We will remove the NA’s for the operations in R like mean etc.., is.na() is used to test objects if they are NA.

9. What is the function used to test objects (returns a logical operator) if they are NaN?
a) as.nan()
b) is.na()
c) as.na()
d) is.nan()

Answer: d
Explanation: is.nan() is used to test if they are NaN. We can check NAN‘s at any stage of the code. We will remove the NA’s for the operations in R. is.nan() is used to test for NaN.

10. What is the function to set column names for a matrix?
a) names()
b) colnames()
c) col.names()
d) column name cannot be set for a matrix

Answer: b
Explanation: colnames() is the function to set column names for a matrix. rownames() is the function to set row names for a matrix. But we can’t use both at a time. Column names and row names can be set separately using the colnames() and rownames() functions.