# R Interview Questions and Answers

Last updated on 05th Oct 2020, Blog, Interview Question

**R** is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing.^{ }The R language is widely used among statisticians and data miners for developing statistical software and data analysis. Polls, data mining surveys, and studies of scholarly literature databases show substantial increases in popularity; as of September 2020, R ranks 9th in the TIOBE index, a measure of popularity of programming languages.

**1.What is R Programming?**

**Ans:**

R is a programming language meant for statistical analysis and creating graphs for this purpose. Instead of data types, it has data objects which are used for calculations. It is used in the fields of data mining, Regression analysis, Probability estimation etc., using many packages available in it.

**2. What are the different data objects in R?**

**Ans:**

** **There are 6 data objects in R. They are vectors, lists, arrays, matrices, data frames and tables.

**3. What makes a valid variable name in R?**

**Ans:**

** **A valid variable name consists of letters, numbers and the dot or underline characters. The variable name starts with a letter or the dot not followed by a number.

**4. What is the main difference between an Array and a matrix?**

**Ans:**

** **A matrix is always two dimensional as it has only rows and columns. But an array can be of any number of dimensions and each dimension is a matrix. For example, a 3x3x2 array represents 2 matrices each of dimension 3×3.

**5. Which data object in R is used to store and process categorical data?**

**Ans:**

** **The Factor data objects in R are used to store and process categorical data in R.

**6. How can you load and use csv file in R?**

**Ans:**

** **A csv file can be loaded using the read.csv function. R creates a data frame on reading the csv files using this function.

**7. How do you get the name of the current working directory in R?**

**Ans:**

** **The command getwd() gives the current working directory in the R environment.

**8. What is the R Base package?**

**Ans:**

This is the package which is loaded by default when R environment is set. It provides the basic functionalities like input/output, arithmetic calculations etc. in the R environment.

**9. How R is used in logistic regression?**

**Ans:**

** **Logistic regression deals with measuring the probability of a binary response variable. In R the function glm() is used to create the logistic regression.

**10. How do you access the element in the 2nd column and 4th row of a matrix named M?**

**Ans:**

The expression M[4,2] gives the element at 4th row and 2nd column.

**11. What is recycling of elements in a vector? Give an example.**

**Ans:**

** **When two vectors of different length are involved in a operation then the elements of the shorter vector are reused to complete the operation. This is called element recycling. Example – v1 <- c(4,1,0,6) and V2 <- c(2,4) then v1*v2 gives (8,4,0,24). The elements 2 and 4 are repeated.

**12. What are different ways to call a function in R?**

**Ans:**

** **We can call a function in R in 3 ways. First method is to call by using the position of the arguments. Second method id to call by using the name of the arguments and the third method is to call by default arguments.

##### Subscribe For Free Demo

**Error:** Contact form not found.

**13. What is lazy function evaluation in R?**

**Ans:**

The lazy evaluation of a function means, the argument is evaluated only if it is used inside the body of the function. If there is no reference to the argument in the body of the function then it is simply ignored.

**14. How do you install a package in R?**

**Ans:**

** **To install a package in R we use the below command.

- install.packages(“package Name”)

**15. Name a R package which is used to read XML files.**

**Ans:**

The package named “XML” is used to read and process the XML files.

**16. Can we update and delete any of the elements in a list?**

**Ans:**

We can update any of the elements but we can delete only the element at the end of the list.

**17. Give the general expression to create a matrix in R.**

**Ans:**

** **The general expression to create a matrix in R is – matrix(data, nrow, ncol, byrow, dimnames)

**18. Which function is used to create a boxplot graph in R?**

**Ans:**

** **The boxplot() function is used to create boxplots in R. It takes a formula and a data frame as inputs to create the boxplots.

In doing time series analysis, what does frequency = 6 means in the ts() function?

Frequency 6 indicates the time interval for the time series data is every 10 minutes of an hour.

**19. What is reshaping of data in R?**

**Ans:**

** **In R the data objects can be converted from one form to another. For example we can create a data frame by merging many lists. This involves a series of R commands to bring the data into the new format. This is called data reshaping.

**20. What is the output of runif(4)?**

**Ans:**

** **It generates 4 random numbers between 0 and 1.

**21. How to get a list of all the packages installed in R ?**

**Ans:**

Use the command

- installed.packages()

**22. What is expected from running the command – strsplit(x,”e”)?**

**Ans:**

** **It splits the strings in vector x into substrings at the position of letter e.

**23. Give a R script to extract all the unique words in uppercase from the string – “The quick brown fox jumps over the lazy dog”.**

**Ans:**

- x <- “The quick brown fox jumps over the lazy dog”
- split.string <- strsplit(x, ” “)
- extract.words <- split.string[[1]]
- result <- unique(tolower(extract.words))
- print(result)

**24. Vector v is c(1,2,3,4) and list x is list(5:8), what is the output of v*x[1]?**

**Ans:**

** **Error in v * x[1] : non-numeric argument to binary operator

**25. Vector v is c(1,2,3,4) and list x is list(5:8), what is the output of v*x[[1]]?**

**Ans:**

** **[1] 5 12 21 32s

**26. What does unlist() do?**

**Ans:**

** **It converts a list to a vector.

**27. Give the R expression to get 26 or less heads from a 51 tosses of a coin using pbinom**.

**Ans:**

- x <- pbinom(26,51,0.5)
- print(x)

**28. X is the vector c(5,9.2,3,8.51,NA), What is the output of mean(x)?**

**Ans:**

** **NA

**29. How do you convert the data in a JSON file to a data frame?**

**Ans:**

Using the function as.data.frame()

**30. Give a function in R that replaces all missing values of a vector x with the sum of elements of that vector?**

**Ans:**

**31. What is the use of apply() in R?**

**Ans:**

** **It is used to apply the same function to each of the elements in an Array. For example finding the mean of the rows in every row.

**32. Is an array a matrix or a matrix an array?**

**Ans:**

Every matrix can be called an array but not the reverse. Matrix is always two dimensional but an array can be of any dimension.

**33. How do you get the standard deviation for a vector x?**

**Ans:**

**34. How do you set the path for the current working directory in R?**

**Ans:**

**35. What is the difference between “%%” and “%/%”?**

**Ans:**

** **“%%” gives remainder of the division of first vector with second while “%/%” gives the quotient of the division of first vector with second.

**36. What does col.max(x) do?**

**Ans:**

** **Find the column has the maximum value for each row.

**37. Give the command to create a histogram.**

**Ans:**

**38. How do you remove a vector from the R workspace?**

**Ans:**

- rm(x)

**39. List the data sets available in package “MASS”**

**Ans:**

- data(package = “MASS”)

**40. List the data sets available in all available packages.**

**Ans:**

**41. What is the use of the command – install.packages(file.choose(), repos=NULL)?**

**Ans:**

It is used to install a r package from a local directory by browsing and selecting the file.

**42. Give the command to check if the element 15 is present in vector x.**

**Ans:**

**43. Give the syntax for creating scatterplot matrices.**

**Ans:**

- pairs(formula, data)

Here formula represents the series of variables used in pairs and data represents the data set from which the variables will be taken.

**44. What is the difference between subset() function and sample() function in R?**

**Ans:**

The **subset()** function is used to select variables and observations.

The **sample()** function is used to choose a random sample of size n from a dataset.

**45. How do you check if “m” is a matrix data object in R?**

**Ans:**

**46. How to obtain the transpose of a matrix in R?**

**Ans:**

The function t() is used for transposing a matrix. Example – t(m) , where m is a matrix.

**47. What is the use of “next” statement in R?**

**Ans:**

** **The “next” statement in R programming language is useful when we want to skip the current iteration of a loop without terminating it.

**48. Which method is used for exporting the data in R?**

**Ans:**

** **There are many ways to export the data into other formats like SPSS, SAS , Stata , Excel Spreadsheet.

**49. Which packages are used for exporting data?**

**Ans:**

For excel xlsReadWrite package is used and for sas,spss ,stata foreign package is implemented.

**50. How impossible values are represented in R?**

**Ans:**

In R NaN is used to represent impossible values.

**51. Which command is used for restoring an R object from a file?**

**Ans:**

** **load command is used for storing R objects from a file.

**Syntax:**

- load(”z.Rdata”)

**52. What is the use of the coin package in R?**

**Ans:**

Coin package is used to achieve the re randomization or permutation based statistical tests.

**53. Which function is used for sorting in R?**

**Ans:**

** **order() function is used to perform the sorting.

**54. What happens when the application object does not handle an event?**

**Ans:**

The event will be dispatched to your delegate for processing.

**55. Explain app specific objects which store the app contents.**

**Ans:**

The app specific objects are Data model objects that store app’s contents.

**56. Explain the purpose of using a UIWindow object?**

**Ans:**

UIWindow object coordinates the one or more views present on the screen.

**57. Tell me the super class of all view controller objects.**

**Ans:**

UIView Controller class.

**58. How to create axes in the graph?**

**Ans:**

Using axes() function custom axes are created.

**59. What is the use of abline() function?**

**Ans:**

** **abline() function is add the reference line to a graph.

**Syntax: **

- abline(h=yvalues, v=xvalues)

**60. Why is the vcd package used?**

**Ans:**

** **vcd package provides different methods for visualizing multivariate categorical data.

**61. What is GGobi?**

**Ans:**

** **GGobi is an open source program for visualization for exploring high dimensional typed data.

**62. What are iPlots?**

**Ans:**

It is a package which provides bar plots, mosaic plots, box plots, parallel plots, scatter plots and histograms.

**63. What is the use of a lattice package?**

**Ans:**

** **The lattice package is to improve on base R graphics by giving better defaults and it has the ability to easily display multivariate relationships.

**64. What is fitdistr() function?**

**Ans:**

It is used to provide the maximum likelihood fitting of univariate distributions. It is defined under the MASS package.

**65. Which data structures are used to perform statistical analysis and create graphs.**

**Ans:**

Data structures are vectors, arrays, data frames and matrices.

**66. What is the use of sink() function?**

**Ans:**

** **It defines the direction of output.

**67. Why is the library() function used?**

**Ans:**

This function is used to show the packages which are installed.

**68. Why is the search() function used?**

**Ans:**

** **By this function we see which packages are currently loaded.

**69. On which type of data binary operators are worked?**

**Ans:**

** **Binary operators are worked on matrices, vectors and scalars.

**70. What is the use of doBY package?**

**Ans:**

It is used to define the desired table using function and model formula.

**71. Which function is used to create a frequency table?**

**Ans:**

** **Frequency table is created by table() function.

**72. Define loglm() function.**

**Ans:**

** **Loglm() function is used to create log-linear models.

**73. What is the use of corrgram() function?**

**Ans:**

corrgram() function is used to plot correlograms.

**74. How to create scatterplot matrices?**

**Ans:**

Pair() or splom() function is used for creating scatterplot matrices.

**75. What is npmc?**

**Ans:**

It is a package which gives nonparametric multiple comparisons.

**R Sample Resumes! Download & Edit, Get Noticed by Top Employers!**Download

**76. What is the use of diagnostic plots?**

**Ans:**

It is used to check the normality, heteroscedasticity and influential observations.

**77. Define anova() function.**

**Ans:**

** **anova() is used to compare the nested models.

**78. What is the cv.lm() function?**

**Ans:**

** **It is defined under the DAAG package which is used for k-fold validation.

**79. Define stepAIC() function.**

**Ans:**

It is defined under the MASS package which performs stepwise model selection under exact AIC.

**80. Define leaps().**

**Ans:**

** **It is used to perform the all-subsets regression and it is defined under the leaps package.

**81. Define relaimpo package.**

**Ans:**

** **It is used to measure the relative importance of each of the predictors in the model.

**82. Why is the car package used?**

**Ans:**

It provides a variety of regression including scatter plots, variable plots and it also enhances diagnostic.

**83. Define robust package.**

**Ans:**

It provides a library of robust methods including regression.

**84. What is robustbase?**

**Ans:**

** **It is a package which provides basic robust statistics including model selection methods.

**85. Define plotmeans().**

**Ans:**

** **It is defined under the gplots package which includes confidence intervals and it produces mean plot for single factors.

**86. What is the full form of MANOVA?**

**Ans:**

** **MANOVA stands for multivariate analysis of variance.

**87. What is the use of MANOVA?**

**Ans:**

** **By using MANOVA we can test more than one dependent variable simultaneously.

**88. Define mshapiro.test( ).**

**Ans:**

** **It is a function which is defined in mvnormtest package. It produces the Shapiro-wilk test for multivariate normality.

**89. Define bartlett.test().**

**Ans:**

** **Bartlett.test() is used to provide a parametric k-sample test of the equality of variances.

**90. What is fligner.test()?**

**Ans:**

It is a function which provides a non-parametric k sample test of the equality of variances.

**91. Which variables are represented by lowercase letters?**

**Ans:**

Numerical variables are represented by lowercase letters.

**92. Which variables are represented by upper case letters?**

**Ans:**

** **Categorical factors are represented by upper case letters.

**93. What is logistic regression?**

**Ans:**

** **Logistic regression is used to predict the binary outcome from the given set of continuous predictor variables.

**94. Define Poisson regression.**

**Ans:**

** **It is used to predict the outcome variable which represents counts from the given set of continuous predictor variables.

**95. Define Survival analysis.**

**Ans:**

** **It includes a number of techniques which are used for modeling the time to an event.

**96. Two vectors X and Y are defined as follows – X <- c(3, 2, 4) and Y <- c(1, 2). What will be output of vector Z that is defined as Z <- X*Y.**

**Ans:**

** **In R language when the vectors have different lengths, the multiplication begins with the smaller vector and continues till all the elements in the larger vector have been multiplied.

The output of the above code will be –

Z <- (3, 4, 4)

**97. Which function in R language is used to find out whether the means of 2 groups are equal to each other or not?**

**Ans:**

**98. What is the value of f (2) for the following R code?**

**Ans:**

- b <- 4
- f <- function (a)
- {
- b <- 3
- b^3 + g (a)
- }
- g <- function (a)
- {
- a*b
- }

The answer to the above code snippet is 35. The value of “a” passed to the function is 2 and the value for “b” defined in the function f (a) is 3. So the output would be 3^3 + g (2). The function g is defined in the global environment and it takes the value of b as 4(due to lexical scoping in R) note 3 returning a value 2*4= 8 to the function f. The result will be 3^3+8= 35.

**99. What is the process to create a table in R language without using external files?**

**Ans:**

- MyTable= data.frame ()
- edit (MyTable)

The above code will open an Excel Spreadsheet for entering data into MyTable.

**100.Which command is used for storing R objects into a file?**

**Ans:**

Save command is used for storing R objects into a file.

**Syntax:**

- save(z,file=”z.Rdata”)

**101.What are the different types of sorting algorithms available in the R language?**

**Ans:**

- Bucket Sort
- Selection Sort
- Quick Sort
- Bubble Sort
- Merge Sort

Are you looking training with Right Jobs?

Contact Us- Artificial Intelligence Tutorial
- Data Science Tutorial
- Machine Learning Tutorial
- Python Interview Questions and Answers
- SAS Interview Questions and Answers

### Related Articles

### Popular Courses

- Python Online Training
`11025 Learners`

- Data Science Course Training
`12022 Learners`

- Machine Learning Online Training
`11141 Learners`

- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know