## Introduction to R Interview Questions And Answers

R is everywhere. Whether it is a Scientist trying to aggregate the numerical data about her experiments or an Analyst conducting regression to solve a business use case, R is the first-choice programming language. In fact, R can do much more than statistics tools, it can be used for data processing, visualizations, and graphics. In this information age, R is the most important language in the Data Science toolkit and it has a huge demand.

So you have finally found your dream job in R but are wondering how to crack the R Interview and what could be the probable 2021 R Interview Questions. Every interview is different and the scope of a job is different too. Keeping this in mind we have designed the most common 2020 R Interview Questions and Answers to help you get success in your interview.

Below is the list of 2021 R Interview Questions and Answers, which can be asked during an interview. These top interview questions are divided into two parts:

### Part 1 – R Interview Questions (Basic)

This first part covers basic R interview questions and answers

#### 1. What is the use of the lm() function?

**Answer:**

‘lm’ stands for a linear model. In R lm() function is used to create regression models. The two most important arguments given to the lm() function are formula and data. The formula defines the regression model and data is the dataset on which the regression is to be conducted.

#### 2. Give an example usage of tapply() method?

**Answer:**

Consider two ordered vectors

1. students distributed across various schools (s1 is the school of the first student, s2 is the school of the second student, etc)

> students <- c(“s1″,”s2″,”s1″,”s3″,”s3″,”s2”)

4.5 (9,549 ratings)

View Course

2. Percentage of each student’s marks

> marks <- c(80,90,75,67,96,67)

> means <- tapply(marks,students,mean)

> means

s1 s2 s3

77.5 78.5 81.5

The function tapply() applies a function ‘mean()’ to the first argument ‘marks’, which is grouped by the second argument ‘students’

Let us move to the next R Interview Questions.

#### 3. How to modify and construct lists? Show with an example?

**Answer:**

Lists Construction:

> Lst <- list(name=”Jack”, age=23, no.cars=3, cars.names = c(“Wagon”, “Bumper”, “Jazz”))

List Modification:

> Lst$cars.names[1] <- “WagonR” OR > Lst[[4]][1] <- “WagonR”

#### 4. What are the different data structures in R?

**Answer:**

There are the basic R interview questions asked in an interview. R has 5 data structures: Vector, Array, Matrix, List, and data frames. Out of which Vectors, Arrays and Matrices are homogenous.

– Vectors are the most common data structure in R. It is a one-dimensional object denoting a set of values. An array is a multi-dimensional generalization of vectors. A matrix is a special case of an array, it is 2-dimensional.

– A list consists of an ordered set of objects which can be of different types or modes. A data frame is like a table or a matrix with columns of different modes.

#### 5. How to deal with missing values in sum(), prod(), min(), max() functions?

**Answer:**

Consider a vector:

> x <- c(3, 6, 2, NA, 1)

Its sum will result in:

> sum(x)

[1] NA

However, we can set the na.rm argument as True to ignore the missing values

> sum(x, na.rm=TRUE)

[1] 12

#### 6. What is the difference between NA and NaN? How do we know if the vector contains either of them?

**Answer:**

NA is equivalent to a missing value. In cases where components of vectors are not completely known, the missing elements are denoted by NA.

On the other hand, the indeterminant values resulting during calculations are denoted by NaN. An example of NaN result could be 0/0.

We can check if a value is NA or NaN using is.na() function. The is.nan(X) function returns true only for NaN.

#### 7. How to write your own functions?

**Answer:**

A function in R can be written as follows:

> function_name <- function(arg1, arg2, . . . ) expression_in_R

expression_in_R is usually a set of different expressions clubbed together.

### Part 2 – R Interview Questions (Advanced)

Let us now have a look at the advanced R Interview Questions.

#### 8. What are matrices in R?

**Answer:**

A Matrix is an array with two subscripts. It is an important special case of array and R provides a lot of functions that are specific to matrices.

For example, t(X) give a transpose of Matrix X, operator %*% is used for matrix multiplication, nrow(X) and ncol(X) give the number of rows and columns, etc

#### 9. How to solve linear equations using matrix inversion?

**Answer:**

Linear equations in matrix form can be represented by:

M * X = C where M is an n x n matrix of coefficients, X is a vector variable of size n and C is a constant vector of size n.

To solve this equation in R, we can use the solve() function as follows:

X = solve(M, C)

Let us move to the next R Interview Questions.

#### 10. What is an inter-quartile range (IQR) and how to calculate it in R?

**Answer:**

Quartiles are the values that divide the data set. Each quartile based on its position in an ordered data-set is called the first (Q1), second (Q2), and third (Q3) quartile. Q2 is the median of the data-set. Q1 is the median of the first-half while Q3 is the median of the upper half of an ordered data set. IQR = Q3-Q1

In R, IQR is calculated by calling the IQR function:

> IQR(dataset)

#### 11. What does the plot() function do?

**Answer:**

These is the frequently asked R interview questions in an interview. The plot is a generic function and depending upon the type of arguments it produces a type of plot. For example,

If x and y are vectors, plot(x, y) produces a scatterplot of y against x.

If z is a list containing two elements x and y or a two-column matrix, plot(z) does the same as above.

#### 12. How to apply a function to all the columns of a data frame?

**Answer:**

We can use the function apply(). It takes in two arguments – the data frame and the function to be applied.

#### 13. How to convert data frames to matrices and why it is required?

**Answer:**

The function as .matrix() is used to convert a data frame into a matrix. R provides powerful libraries that are specific to matrices. Hence, data frames converted to matrices can be analyzed using these matrix formulae.

Let us move to the next R Interview Questions.

#### 14. How to format character arrays into dates in R?

**Answer:**

You can use the function as.Date() which takes a vector of character arrays and a format to convert them into a date object.

For example,

> as.Date(“22:2:2001″,format=”%d:%m:%Y”)

#### 15. Find the smallest and the largest number between 7000 and 70000 that is divisible by 233.

**Answer:**

> Find(function(x) x %% 233 == 0, 7000:70000)

[1] 7223

> Find(function(x) x %% 233 == 0, 7000:70000, right = TRUE)

[1] 69900

### Conclusion

We have covered interview questions pertaining to some of the most common concepts in R. As R supports an extensive library, working on R often is a continuous learning process. Furthermore, you can stay in touch with the R-Community and check out the additional resources on CRAN. All the best for your interview!

### Recommended Article

This has been a guide to List Of R Interview Questions and Answers so that the candidate can crackdown these R Interview Questions easily. You may also look at the following articles to learn more –