Top R Interview Questions And Answers

Question 1: What is R Programming?
  • R Programming language was created by Ross Ihaka and Robert Gentleman, at the University of Auckland.
  • R Programming language is generally used for developing statistical analysis, graphics representation, and reporting.
  • Now, R is developed by the R Development Core Team.

Question 2: What are the different data objects in R?

Data Objects in R are listed below:
  • vectors
  • lists
  • arrays
  • matrices
  • data frames
  • tables

Question 3: What makes a variable name valid in R?

  • Variable name starts with an alphabet letter
  • Variable consisting of number, dot, and underscore
Question 4: What is the main difference between an array and a matrix?

 Matrix   Array
 Matrix can be only two dimensional  Array can be multi-dimensional
 Matrix has rows and columns  Array can be represented by multiple matrices
Question 5: Which data object in R is used to store and process categorical data?

In R Programming, factor data objects are used to store and process categorical data.

Question 6: How can you load and use CSV files in R?
In R Programming language, read.cvs method is used to load CSV files.
Question 7: How do you get the name of the current working directory in R?

In R Programming, getwd() method is used to get the current working directory.

Question 8: What is R Base package?

  • R Base package basically provides input/output functionality and arithmetic calculation functionality.
  • R Base package is automatically installed at the time of R Programming Environment setup.
Question 9: How is R used in logistic regression?

In R Programming, glm() method is used to create logistic regression and logistic regression works with measuring the probability of a binary response variable.

Question 10: How do you access the element in the 3rd column and 1st row of a matrix named M?

We can print 1st row and 3rd column using the command print(m[1,3])
  1. M = matrix( c(‘a’,‘a’,‘b’,‘c’,‘b’,‘a’), nrow = 2, ncol = 3, byrow = TRUE)
  2. print(M[1,3])


Question 11: What is the recycling of elements in a vector? Give an example.

In R Programming language, recycling of elements is when we perform operations on two different vectors having different lengths. In it, the elements of the shorter length vector are used to complete the operation.

  1. v1 <- c(4,1,0,6)
  2. v2 <- c(2,4)
  3. print(v1*v2)


Question 12: What are different ways to call a function in R?
They are listed below
  • Calling a function with argument values (by position and by name)


  1. # Create a function with arguments.
  2. <- function(x,y,z) {
  3.    res <- x * y + z
  4.    print(res)
  5. }
  6. # Call the function by position of arguments.
  8. # Call the function by names of the arguments.
  9. = 11, y = 5, z = 3)


  • Calling a function with default argument


  1. # Create a function with arguments.
  2. <- function(a = 10, b = 20) {
  3.    res <- a * b
  4.    print(res)
  5. }
  6. # Call the function without giving any argument.
  8. # Call the function with giving new values of the argument.


  • Calling a function without an argument


  1. # Create a function without an argument.
  2. <- function() {
  3.    for(j in 1:5) {
  4.       print(j^2)
  5.    }
  6. }
  7. # Call the function without supplying an argument.


Question 13: What is lazy function evaluation in R? 
Argument of function is only executed when they are needed.
  1. # Create a function with arguments.
  2. <- function(x, y) {
  3.    print(x^2)
  4.    print(x)
  5.    print(y)
  6. }
  7. # Evaluate the function without supplying one of the arguments.


Question 14: How do you install a package in R? 
In R programming, the package is installed using the following command.
  1. install.packages(“package Name”)

Question 15: Name a R package that is used to read XML files?

XML package is used to read XML files.
  1. # Load the package required to read XML files.
  2. library(“XML”)
  3. # Also load the other required package.
  4. library(“methods”)
  5. # Give the input file name to the function.
  6. res <- xmlParse(file = “input.xml”)
  7. # Print the result.
  8. print(res)


Question 16: How can we update and delete any of the elements in a list?

In R Programming language, element is deleted from the last of the list but we can update any element in the list.
  1. list1 <- list(c(“c++”,“c#”,“”), matrix(c(1,2,3,4,5,6), nrow = 2),
  2.    list(“red”,20.3))
  3. names(list1) <- c(“R”“c# corner”“.net”)
  4. list1[4] <- “R programming”
  5. print(list1[4])
  6. list1[4] <- NULL
  7. print(list1[4])
  8. list1[3] <- “R programming language”
  9. print(list1[3])


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

Syntax of matrix creation
: matrix(data, nrow, ncol, byrow, dimnames)
  1. # Elements are arranged sequentially by row.
  2. M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
  3. print(M)
  4. # Elements are arranged sequentially by column.
  5. N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
  6. print(N)
  7. # Define the column and row names.
  8. rownames = c(“row1”“row2”“row3”“row4”)
  9. colnames = c(“col1”“col2”“col3”)
  10. P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
  11. print(P)


Question 18: Which function is used to create a boxplot graph in R? 
In  R Programming, Boxplot method is used to create boxplot graph.
  1. # Give the chart file a name.
  2. png(file = “boxplot.png”)
  3. # Plot the chart.
  4. boxplot(mpg ~ cyl, data = mtcars, xlab = “Number of Cylinders”,
  5.    ylab = “Miles Per Gallon”, main = “Data”)
  6. # Save the file.


Question 19: What is reshaping of data in R?

In R Programming, reshaping of data is the conversion of one data object into other data object.
Question 20: What is the output of runif(4)?
runif() method return random number (0-4)
  1. print(runif(4))


Question 21: How to get a list of all the packages installed in R ?
In R Programming, we install the list package using the following command.
  1. installed.packages()

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

This method splits string in vector x in to a substring at the position of substring e.
Question 23: Vector v is c(1,2,3,4) and list x is list(5:8), what is the output of v*x[1]?
Error: non numeric argument.
Question 24: What does unlist() do?
This function converts a list into vector.
Question 25: Give the R expression to get 26 or less heads from  51 tosses of a coin using pbinom.?
  1. x <- pbinom(26,51,0.5)
  2. print(x)


Question 26: How do you convert the data in a JSON file to a data frame?
In R Programming, data in a JSON file can be converted using method.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s