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])
Example
  1. M = matrix( c(‘a’,‘a’,‘b’,‘c’,‘b’,‘a’), nrow = 2, ncol = 3, byrow = TRUE)
  2. print(M[1,3])

Output

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.

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

Output

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)

Example

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

Output

 
  • Calling a function with default argument

Example

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

Output

  • Calling a function without an argument

Example

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

Output

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

Output

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.
Example
  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)

Output

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.
Example:
  1. list1 <- list(c(“c++”,“c#”,“asp.net”), 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])

Output

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

Syntax of matrix creation
: matrix(data, nrow, ncol, byrow, dimnames)
Example
  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)

Output

Question 18: Which function is used to create a boxplot graph in R? 
In  R Programming, Boxplot method is used to create boxplot graph.
Example
  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.
  7. dev.off()

Output

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)
Example
  1. print(runif(4))

Output

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.?
Example
  1. x <- pbinom(26,51,0.5)
  2. print(x)

Output

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 as.data.frame() method.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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