250+ TOP MCQs on ggplot2 and Answers

R Programming Problems on “ggplot2 ”.

1. _________ display contours of a 3d surface in 2d.
a) aes_contour
b) geom_contour
c) aes_gem
d) aes_auto

Answer: b
Clarification: A layer specific dataset – only needed if you want to override the plot defaults.

2. Point out the correct statement?
a) autoplotgraph is used to complete ggplot appropriate to a particular data type
b) auto_element wraps up a projection of summary functions
c) ggplot.data create a new ggplot plot from a data frame
d) aes_sdensity display a smooth density estimate

Answer: c
Clarification: is.ggplot reports whether x is a ggplot object.

3. ______ display a smooth density estimate.
a) geom_density2
b) geom_density
c) aes_sdensity
d) geom_contour

Answer: b
Clarification: geom_density display a smooth density estimate. A smooth density estimate calculated by stat_density.

4. Which of the following draws nothing?
a) geom_blank
b) geom
c) geom_bin2d
d) geom_contour

Answer: c
Clarification: geom_bin2d adds heatmap of 2d bin counts.

5. Point out the correct statement?
a) is.theme reports whether x is a real object
b) is.object reports whether x is a aesthetic object
c) qplot is used for quick plot
d) ggplot describe the type of plot you will produce

Answer: c
Clarification: is.rel reports whether x is a real object.

6. _________ describe the type of plot you will produce.
a) geoms
b) ggplot
c) fplot
d) gplot

Answer: a
Clarification: Geoms is short form for geometric objects.

7. __________ is interval represented by a vertical line, with a point in the middle.
a) geom_range
b) geom_pointrange
c) printplot
d) geom_contour

Answer: b
Clarification: autoplot uses ggplot2 to draw a particular plot for an object of a particular class in a single command.

8. Which of the following create a set of identity mappings?
a) ggplot
b) aes_all
c) aes
d) ggorder

Answer: d
Clarification: Character vector is given for the creation of identity.

250+ TOP MCQs on Linear Regression and Answers

R Programming Language Multiple Choice Questions on “Linear Regression ”.

1. In practice, Line of best fit or regression line is found when _____________
a) Sum of residuals (∑(Y – h(X))) is minimum
b) Sum of the absolute value of residuals (∑|Y-h(X)|) is maximum
c) Sum of the square of residuals ( ∑ (Y-h(X))2) is minimum
d) Sum of the square of residuals ( ∑ (Y-h(X))2) is maximum

Answer: c
Clarification: Here we penalize higher error value much more as compared to the smaller one, such that there is a significant difference between making big errors and small errors, which makes it easy to differentiate and select the best fit line.

2. If Linear regression model perfectly first i.e., train error is zero, then _____________________
a) Test error is also always zero
b) Test error is non zero
c) Couldn’t comment on Test error
d) Test error is equal to Train error

Answer: c
Clarification: Test Error depends on the test data. If the Test data is an exact representation of train data then test error is always zero. But this may not be the case.

3. Which of the following metrics can be used for evaluating regression models?

i) R Squared
ii) Adjusted R Squared
iii) F Statistics
iv) RMSE / MSE / MAE

a) ii and iv
b) i and ii
c) ii, iii and iv
d) i, ii, iii and iv

Answer: d
Clarification: These (R Squared, Adjusted R Squared, F Statistics, RMSE / MSE / MAE) are some metrics which you can use to evaluate your regression model.

4. How many coefficients do you need to estimate in a simple linear regression model (One independent variable)?
a) 1
b) 2
c) 3
d) 4

Answer: b
Clarification: In simple linear regression, there is one independent variable so 2 coefficients (Y=a+bx+error).

5. In a simple linear regression model (One independent variable), If we change the input variable by 1 unit. How much output variable will change?
a) by 1
b) no change
c) by intercept
d) by its slope

Answer: d
Clarification: For linear regression Y=a+bx+error. If neglect error then Y=a+bx. If x increases by 1, then Y = a+b(x+1) which implies Y=a+bx+b. So Y increases by its slope.

6. Function used for linear regression in R is __________
a) lm(formula, data)
b) lr(formula, data)
c) lrm(formula, data)
d) regression.linear(formula, data)

Answer: a
Clarification: lm(formula, data) refers to a linear model in which formula is the object of the class “formula”, representing the relation between variables. Now this formula is on applied on the data to create a relationship model.

7. In syntax of linear model lm(formula,data,..), data refers to ______
a) Matrix
b) Vector
c) Array
d) List

Answer: b
Clarification: Formula is just a symbol to show the relationship and is applied on data which is a vector. In General, data.frame are used for data.

8. In the mathematical Equation of Linear Regression Y = β1 + β2X + ϵ, (β1, β2) refers to __________
a) (X-intercept, Slope)
b) (Slope, X-Intercept)
c) (Y-Intercept, Slope)
d) (slope, Y-Intercept)

Answer: c
Clarification: Y-intercept is β1 and X-intercept is – (β1 / β2). Intercepts are defined for axis and formed when the coordinates are on the axis.

250+ TOP MCQs on Basics of R Programming and Answers

R Programming Language Multiple Choice Questions on “Basics”.

1. A single element of a character vector is referred as ________
a) Character string
b) String
c) Data strings
d) Raw data

Answer: a
Clarification: Single element of a character vector is often referred to as a character string. Dates are represented by the Date class and can be coerced from a character string using the as.Date() function. This is a common way to end up with a Date object in R.

2. R files has an extension ______
a) .R
b) .S
c) .Rp
d) .c

Answer: a
Clarification: All R files have an extension .R. R provides a mechanism for recalling and re-executing previous commands. All S programmed files will have an extension .S. But R has many functions than S.

3. If the code is stored in the external file, which of the following function is used to call them to a working directory?
a) exec()
b) source()
c) execute()
d) sourcefile()

Answer: b
Clarification: source is available in the file menu. Today R runs on almost any standard computing platform and operating system. Its open source nature. The copyright for the primary source code for R is held by the R Foundation and is published under the GNU General Public License version.

4. Advanced programmers can write ______ code to manipulate R objects.
a) Python
b) Java
c) C
d) Java Script

Answer: c
Clarification: C, C++ and Fortran can be linked. There are a number of people had used something like C/C++ programming languages to manipulate the R objects.

5. In ________ Insightful purchased the S language from Lucent for $2 million.
a) 2002
b) 2003
c) 2004
d) 2005

Answer: c
Clarification: TIBCO is the current owner of the S language. In 2008, Insightful was acquired by TIBCO for $25 million. As of this writing, TIBCO is the current owner of the S language and is its exclusive developer.

6. Functionality of R is divided into a number of __________
a) Functions
b) Domains
c) Packages
d) Files

Answer: a
Clarification: CRAN hosts many add-on packages that can extend the functionality of R. Connections allow R functions to talk to all different external objects without having to write custom code for each object.

7. Dataframes can be converted into a matrix by calling the following function data ______
a) matr()
b) matrix()
c) matrixf()
d) matrixfunc()

Answer: b
Clarification: as.matrix function should be used to coerce a data frame to a matrix. It might seem that the as.matrix() function should be used to coerce a data frame to a matrix.

8. What will be the output of the following R code?

       x <- (“a”, “b”)
       as.logical(x)

a) a b
b) “a” “b”
c) 1 1
d) Error

Answer: d
Clarification: It is not possible to coerce, so you will get a Warning. Logical creates a logical vector of the specified length. Each element of the vector is equal to FALSE. as.logical attempts to coerce its argument to be of a logical type.

9. What will be the output of the following R code?

      a <- (“a” , “b”)
      mode(a)

a) Character
b) Numeric
c) Complex
d) Integer

Answer: a
Clarification: Mode of the given command is a character. There are a complementary function writeLines() that takes a character vector and writes each element of the vector one line at a time to a text file.

10. Which of the following is not an object of R?
a) calls
b) expressions
c) strings
d) names

Answer: c
Clarification: Calls, expressions and names are the three types of objects that constitute the R language. There is really only one rule about vectors in R, which is that A vector can only contain objects of the same class. R objects can have names, which is very useful for writing readable code and self-describing objects.

250+ TOP MCQs on Connection Interfaces and Answers

R Programming Language Multiple Choice Questions on “Connection Interfaces”.

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

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

2. 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
Clarification: It’s better to stick with a binary format for efficiency and accuracy.

3. 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
Clarification: You can save all objects in your workspace using the save.image() function.

4. 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
Clarification: .rda and .RData are fairly common extensions and you may want to use them because they are recognized by other software.

5. 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
Clarification: Output may get sent to a file, but it could get sent over a network or other connection.

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

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

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

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

8. 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
Clarification: Open is used for opening connection to ‘foo.txt’ in read-only mode.

9. 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”)

Answer: a
Clarification: Connections must be opened, then the are read from or written to, and then they are closed.

10. 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”)

Answer: b
Clarification: For more structured text data like CSV files or tab-delimited files, there are other functions like read.csv() or read.table().

250+ TOP MCQs on Functions and Answers

R Programming Objective Questions & Answers focuses on “Functions”.

1. If two vectors with different lengths perform some operation, the elements of the shorter vector will be used again to complete the operation. This is referred to as _________________
a) Element Recycling
b) Recycling
c) Cycling
d) Element Cycling

Answer: a
Clarification: If two vectors with different lengths perform an operation, the elements of the shorter vector will be used again to complete the operation. This is referred to as element recycling.

2. If the function in a console is.matrix(X) returns true then X can be considered as a _______
a) Matrix object
b) Matrix data object
c) Matrix vector
d) Vector

Answer: b
Clarification: The function call is.matrix(X) returns TRUE then X can be termed as a matrix data object. R has a large number of in-built functions and also the user can create their own functions.

3. ______________ measures the probability of the binary response variable in R language.
a) Logical Regression
b) Multivariate Regression
c) Simpler Regression
d) Statistics

Answer: a
Clarification: Logistic regression can be used for this and the function glm () in R language provides this functionality. Logistic regression is a statistical method for analysing a dataset in which there are one or more independent variables which determine an outcome.

4. ______________ function will measure the probability of the binary response variable in R language.
a) Glm()
b) Glmi()
c) Gelmi()
d) Jlm()

Answer: a
Clarification: Glm () in R language provides this functionality. Logistic regression is a statistical method for analysing a dataset in which there are one or more independent variables that determine an outcome.

5. ____________ function can be used to select the random sample of size ‘n’ from a huge dataset.
a) Simple()
b) Sample()
c) While()
d) Signal()

Answer: b
Clarification: Sample () function can be used to select a random sample of size ‘n’ from the huge dataset. R has a large number of in-built functions and also the user can create their own functions.

6. Which function is used to select variables and observations from a given dataset?
a) Subset()
b) Sample()
c) While()
d) Signal()

Answer: a
Clarification: Subset () function is used to select variables and observations from a given dataset. R has a large number of in-built functions and also the user can create their own functions.

7. ____________ package in R language provides various options for re-randomization and permutations based on statistical tests.
a) Coin
b) Vain
c) Join
d) Lain

Answer: a
Clarification: Coin package in R language provides various options for re-randomization and permutations based on statistical tests. When test assumptions cannot be met then this package serves as a good alternative to classical methods.

8. A matrix of scatterplots can be produced using _________ function.
a) Pears
b) Pairs
c) Tears
d) Cars

Answer: b
Clarification: A matrix of scatterplots can be produced using pairs. Pairs function takes various parameters like formula, data, subset, labels, etc.

9. __________ function takes various parameters like formula, data, subset, labels, etc.
a) Pears
b) Pairs
c) Tears
d) Cars

Answer: b
Clarification: Pairs function takes various parameters like formula, data, subset, labels, etc. A matrix of scatterplots can be produced using pairs.

10. How will you check if an element is present in a vector?
a) Match()
b) Dismatch()
c) Mismatch()
d) Search()

Answer: a
Clarification: It can be done using the match () function- match () function returns the first appearance of a particular element. The other way is to use %in% which returns a Boolean value either true or false.

250+ TOP MCQs on Data Wrangling and Answers

R Programming Language Multiple Choice Questions on “Data Wrangling”.

1. _________ is new package that makes it easy to “tidy” your data.
a) tidy
b) tidyr
c) tidyneat
d) tidynr

Answer: c
Clarification: Tidy data is data that’s easy to work in R.

2. Point out the correct statement?
a) Each row is an observation in tidy data
b) Each column is a variable in tidy data
c) Arranging your data in tidy way makes it easier to work
d) All of the mentioned

Answer: d
Clarification: When use tidy data and tidy tools, you spend less time worrying about how to feed the output from one function into the input of another.

3. Which of the following is complementary to tidyr?
a) geolr
b) dplyr
c) d3lr
d) gekl2

Answer: b
Clarification: Its easy to munge with dplyr.

4. How many functions exist for tidying the data?
a) one
b) two
c) three
d) four

Answer: c
Clarification: tidyr provides three main functions for tidying your messy data: gather(), separate() and spread().

5. Point out the correct statement?
a) gather() makes “wide” data longer
b) Two functions are present for tidy data
c) separate() makes “wide” data longer
d) spread() function takes multiple columns

Answer: c
Clarification: Other names for gather include melt (reshape2), pivot (spreadsheets) and fold (databases).

6. Which of the following function takes multiple columns?
a) spread()
b) gather()
c) separate()
d) extract()

Answer: b
Clarification: gather() gathers column into key-value pairs.

7. __________ uses regex groups instead of a splitting pattern or position.
a) spread()
b) gather()
c) separate()
d) extract()

Answer: c
Clarification: separate() allows you to tease columns apart.

8. Which of the following function works similar to separate()?
a) extract()
b) gather()
c) sep()
d) separate()

Answer: a
Clarification: extract() works in a similar way.

9. Which of the following d takes two columns and spreads them into multiple columns?
a) ggmissplot
b) printplot
c) print.ggplot
d) ggplot

Answer: c
Clarification: spread() makes “long” data wide.