Updated May 6, 2023
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. 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 essential language in the Data Science toolkit and 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 the probable 2023 R Interview Questions could be. Every interview is different, and the job scope is different too. Keeping this in mind, we have designed the most common 2023 R Interview Questions and Answers to help you get success in your interview.
Below is the list of 2023 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.
Q1. 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 for the lm() function are formula and data. The formula defines the regression model, and the data is the dataset for the regression.
Q2. 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”)
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’
Q3. How to modify and construct lists? Show with 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”
Q4. What are the different data structures in R?
Answer:
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 of different types or modes.
- A data frame is like a table or a matrix with columns of different modes.
Q5. 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 the following:
> 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
Q6. 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 entirely known, the missing elements are denoted by NA.
On the other hand, NaN denotes the indeterminate values resulting from calculations. An example of a 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.
Q7. 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.
Q8. What are matrices in R?
Answer:
A Matrix is an array with two subscripts. It is an important case of an array, and R provides many functions 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
Q9. 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)
Q10. What is an interquartile 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
To calculate IQR in R, you can call the IQR function.
> IQR(dataset)
Q11. What does the plot() function do?
Answer:
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.
Q12. How to apply a function to all the columns of a data frame?
Answer:
We can use the function apply(). It takes two arguments – the data frame and the function to be applied.
Q13. How to convert data frames to matrices, and why is it required?
Answer:
The function as .matrix() converts a data frame into a matrix. R provides powerful libraries that are specific to matrices. You can analyze data frames converted to matrices using these matrix formulae.
Q14. How to format character arrays into dates in R?
Answer:
You can use the function as.Date() 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”)
[1] “2001-02-22”Q15. Find the smallest and the largest numbers between 7000 and 70000 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 about 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 Articles
This has been a guide to List Of R Interview Questions and Answers so that the candidate can easily crack down on these R Interview Questions. You may also look at the following articles to learn more –