R Language Interview Questions and Answers for Freshers
Posted by Superadmin on August 22 2020 07:12:10

R Language Interview Questions and Answers for Freshers

 

Q.1. When should you apply “next” statement in R? When is it appropriate to use the “next” statement in R?

A data scientist will use next to skip an iteration in a loop. As an example:

  1. > val1 <- 1:30 
  2. > for(val in val1){ 
  3. + if(val == 25){ 
  4. + next
  5. + } 
  6. + print(val) 
  7. + }

This piece of code will iterate through the numbers from 1 to 30. It will skip 25 as we have used the next statement to skip the iteration from which we move on to the next value. We will obtain the output from 1-24 and 26-30.

Q.2 What is the value of equation1(3) for the following R code?

  1. > num <- 4 
  2. > equation1 <- function (val) 
  3. + { 
  4. + num <- 3 
  5. + num^3 + g (val) 
  6. + } 
  7. > equation2 <- function (val) 
  8. + { 
  9. + val*num
  10. + } 
  11.  
  12. }

For the above code snippet, we obtain the output as 39.

Q.3 What arguments are required in the R6Class?

In order to create an R6 class in R, the following arguments are required –

Did you answer the above question? If no, you can revise the concept of arguments in R with DataFlair’s detailed guide.

Q.4 R suffers from the disadvantage of being restricted to the local memory. Could you elaborate the precise memory limit in R?

For a 32-bit, R program is executed, the maximum value that can be obtained is under 4 GB.

Furthermore, for systems that are 64-bits, the limit is just 8 TB.

Q.5 regmatches(number, gregexpr(pattern = “2.?2”,text = number)) Is this an example of non-greedy quantifier? If yes, then why and what would be the equivalent greedy quantifier?

No. This is an example of a non-greedy quantifier. An equivalent greedy approach would be –

  1. regmatches(number, gregexpr(pattern = "2.*2",text = number)) .

Suppose that for a given number, val1 = 2020000200

The greedy approach would initiate from the first digit ‘2’. It then moves ahead and encounters the second digit ‘2’. It keeps searching for another ‘2’ digit which it also encounters. However, it does not find any more digits after that and hence, it returns the value: 20200002.

A non-greedy approach on the other hand, stops at the first match and returns us the value of 202.

Q.6 A csv file consists of missing values that are represented by hashtags (“#”) and ampersands (“&”). How can you read this type of a csv file in R?

We can read this type of data with the following line of code –

  1. csv2(‘data-file.csv’,header=FALSE,sep=’,’,na.strings=c(#’,’&’))

Q.7 Suppose that you have an array consisting of several objects. This object is of the type string. Let this string be “DataFlair”. Now, you want to find the location of this string in the character array ‘arr’. How will you carry this out?

which( arr == “DataFlair”)

Learn everything about R arrays in detail 

Q.8 Suppose that while writing the R code that consists of multiple functions. While executing the code, there is an error within one of the functions present in the code. What command will you use to find the function that is facing problem?

  1. traceback()

Q.9 You are solving a data science problem that is ridden with many missing values. You want to clean the data before fitting your model. What would you do to replace these missing values? Develop your own function.

Let’s take this vector:

  1. arr<-c(1,2,4,NA,NA,11,NA,10)
  2. Now, let’s write the function to impute the values:
  3. missing_mean <- function(arr){
  4. ifelse(is.na(arr),mean(arr,na.rm = T),arr)
  5. }

Don’t forget to check DataFlair’s detailed tutorial on R Vectors

Q.10 You have been assigned the task of reshaping the data wherein you have to convert the wide format data into long format data and vice versa. How will you carry out this operation?

melt() and dcast()

Q.11 You have two tables “employee_salary” and “employee_experience”. The first table consists of two columns “Name” and “Salary”. The second table consists of “Name” and “Experience” columns. How will you merge these two tables to create a single table that does not have any redundant column in it?

In order to join these two tables, we will have to perform their cross-product. R provides the function “merge” that can be used for the same.

  1. merge(employee_salary,employee_experience,by=NULL)

Q.12 How will you use rattle package in R?

Rattle package provides a graphical user interface to the other R packages provides functionality for data mining operations. After installing the Rattle Package, we type rattle() in the R Studio’s Script Panel.

  1. rattle()

Q.13 How do tibbles differ from the traditional data-frames?

The two main areas that differentiate tibble from the traditional data-frames are – printing and subsetting:

Tibbles consists of a refined print method that displays the first 10 rows as well as the columns that can be fit on the screen. This facilitates easy working with the large data. In addition to the name, each column reports the type of tibble.

The second area where Tibbles differ from data-frames is subsetting. Tibbles have a strict notion about subsetting. If we access a variable that does not exist, we will get an error.

Q.14 How is seq_along(5) different from seq(5)?

The seq_along(6) will result in a vector that has a length of 6 whereas seq(6) will deliver us a consecutive vector from 1 to 6c( (1,2,3,4,5,6)).

Q.15 Suppose that I want to convert factor to number. I write the following code:

  1. fac <- factor(c(2.3, 1.5, 3, 4.9))
  2. as.numeric(fac)

I obtain the output as – 2,1,3,4 which is clearly not what was expected. How should I rectify this problem?

In the above code, you are converting factor to number which is problematic. R simply floors the values without any warning. In order to convert factors to numbers properly, we use the integer vector to perform indexing on the factor levels as follows –

  1. levels(fac)[fac]

Have you checked R factor analysis

Q.16 Suppose that I want to know the values in c(1, 2, 6, 3, 19) that are not present in c(2, 6, 14, 3, 15). How can you carry this out using built-in function as well as without it?

There are two methods to execute this problem –

Q.17

R data science interview questions

The above data table is written to a csv file called ‘Datatable.csv’. Suppose you want to read this csv file with all 3 rows. How will you carry this out?

  1. csv2(‘Datatable.csv’,header=FALSE,sep=’,’)

Q.18 You have a dataset from which you want to extract a subset. For example, for a data consisting of employee details, you want to create a subset of employees who are above 30 years and who make salary less than 10,000. How will you execute this in R? The salary dataframe be denoted by the variable ‘age’ and the data is contained within the variable ‘employee_data’.

To create a subset of employees older than 30, we will make use of the filter() function as follows –

  1. filter(employee_data, age >30 1, salary < 10000)

Q.19 Suppose you have a vector of numbers vec <- (1,3,4,7) that needs to be combined with the symbols (“#”,”?”) consecutively such that we obtain the output 1-#, 3-?, 4-#, 7-? How will you proceed with this?

Ans. We will use the paste() function as follows –

  1. paste(vec,c("#","?"),sep = "-")

Q.20 What are the differences between t.test() and t.data.frame()? When do you use these two functions?

Ans. Due to the naming scheme of S3’s generic class, both functions are similar to one another. However, they are not related to each other.

As a result of S3 dispatch rules, t.test() along with t() would get called when the latter is applied to the class “test”

Q.21 What does the following code denote about index updation?

  1. for (index in 1:3) {
  2. index <- index * 2
  3. print(index)
  4. }

Ans. In a for-loop the index is updated at the beginning of each iteration. Therefore, reassigning the index symbol during one iteration doesn’t affect the following iterations.

Q.22 Why do we obtain five missing values from the following lines of code?

  1. x <- 1:5
  2. x[NA]

R programming interview and answers

Ans. NA is a logical type object and the logical vectors are reused to the equal length shared between vector and its subset. For example, x[NA] when iterated five times becomes x[NA, NA, NA, NA, NA].

Q.23 Is it possible to model transitioning data like time-zone or a working directory? State reason behind your answer.

Ans. No. S3 models are not adept at capturing models that transition over a period of time. This is because the S3 methods are designed to take the exact input and provide the same result.

Q.24 Is it possible to plot all the features of a dataset at once? If so, how?

Ans.

  1. > library(tabplot) #DataFlair
  2. > tableplot(iris)

R programming interview questions and answers

Q.25 What are is.atomic(), is.vector() and is.numeric() functions responsible for?

Ans. These three functions are responsible for –

Recommended Reading – R Functions Tutorial 

Q.26 How can we test if the time-series model is stationary in R? Take the first column and all the rows of EuStockMarkets as your time-series data.

Ans. For testing if the time-series model is stationary, we make use of the Augmented Dickey Fuller Test (adf test). If the p-value is below 0.05, then that model is stationary. We can implement this in R as follows –

library(tseries)
adf.test(timedata)
kpss.test(timedata)

R programming practice questions

Q.27 How will you explain R programming to person from a non-technical background?

Ans. R is a programming language that many data analysts, data scientists, statisticians utilize to analyze data and perform statistical analysis using graphs and other forms of visualizations. Using R, one can analyze large datasets. It is an ever-expanding programming language with thousands of packages that provide support to a variety of applications.

This is the frequently asked R interview question. You can learn or master any concept in R with DataFlair’s Free R Tutorials Library

Q.28 Suppose your data science project requires the installation of the ggplot2 package. How will you install it?

Ans. We can perform installation of R packages using a common syntax –

  1. install.packages(“package-name”). In the case of ggplot2, it will be -
  2. install.packages(“ggplot2”)

Practice the latest Data Science Projects for your upcoming interviews.

Q.29 What is RShiny. What are the several advantages of using it?

Ans. Using the Shiny package, you can build interactive web applications. Following are the advantages of RShiny.

Q.30 What do you know about RMarkdown?

Ans. RMarkdown provides a notebook interface to the R user. We generally use it to produce high-quality reports for sharing it with the audience. It supports several static as well as dynamic output formats. Using RMarkdown, one can present the code alongside its output (graph, plot, tables) with conventional text to explain the program. The RMarkdown makes use of the Markdown syntax which is a markup language providing methods for the creation of documents with header, links, images etc. from the plain text files while maintaining the original plain text.

Q.31 What are the various sorting functions available in R?

Ans. R provides us with the sort() function that we can use for carrying out sorting of the data. Within this function, there are several parameters through which different sorting operations can be carried out. R provides support for two types of sorting –

The first type of sort is comparison-based sorting wherein the algorithm compares key values of the input vector with the other input which takes place before ordering the data.

Non-comparison based sorting is the second type where the algorithm performs computation on each key-value which then proceeds to perform ordering on the computed values.

The three sorting algorithms are –
>Insertion sort
>Bubble sort
>Selection sort