10X Sale
kh logo
All Courses

Introduction

R is widely used for data analysis tasks, such as data manipulation, statistical analysis, data visualization, and machine learning. Its ability to handle large datasets, perform complex statistical analyses, and create data visualizations. Prepare for your R interview with the top R interview questions curated by our experts. This will help convert your R interview into a top job offer as a Business Statistical Analyst, R programmer, or any other beginner, intermediate or expert role. The following list covers the conceptual questions for freshers and experts and helps you cover topics like the difference between dcast() and table(), tidy data in R, etc, giving you an edge in the data analytics market. Prepare well with these R programming interview questions and answers and ace your next interview. You can also convert this resource as your R programming interview questions PDF and use it for quick revision.

R Programming Interview Questions and Answers
Beginner

1. Given data related to specific parameter for a country for a particular year is mentioned in Table 1. Provide an approach or write a program function using R to reshape the data in the way it is expected in Table 2 (which is the desired layout). Explain briefly with your response.

Table 1 (Given input data)

Country201120122013
Japan230031006800
China270033005400
India480062009500

Assume this data exists in your data frame in R as “my_df”

Table 2 (Expected desired layout as output)

CountryYearn
Japan20112300
China20112700
India20114800
Japan20123100
China20123300
India20126200
Japan20136800
China20135400
India20139500

Here objective is to get the count (n) captured in a separate row for every year for every country.

We can use gather() function in tidyr package to accomplish this.

Below is the desired line of code.

# This will load the “tidyr” package
library(tidyr)
# This will reshape the data in desired format
gather(my_df,"Year","n",2:4,convert = TRUE)

gather() function parameters –

  • my_df is the first parameter to reshape the data.
  • “Year” is the second parameter which is name of the new key column, typically this is a character string.
  • “n” is the third parameter which is the name of the new value column.
  • 2:4 is the fourth parameter which shows names or numeric indexes of columns to collapse from your input dataset .
  • “Convert=TRUE” is the last parameter mentioned here which converts number in the keys column from factors to numeric.

This is one of the most frequently asked R programming interview questions for freshers and experienced professionals in recent times. 

2. Given sample data below in table 1 has 4 columns including a date column in “Col4”. Provide an approach using separate() function in R to convert the data to be reflected in desired layout as provided in Table 2. Explain briefly.

Table1 (Input data layout)

Assume this data exists in your data frame in R as “my_df”

Table 2 (Expected desired layout as output)

We can use the following approach using separate to distribute date field into three separate columns for year, month and day values.

# This will load the tidyr package
library(tidyr)
# This will reshape the data in desired format
separate(my_df, Col4, c("year","month","day"),sep = "-")

separate() function will use the parameters appropriately to display data in desired format.

  • First parameter used here is the data frame which is my_df.
  • Second parameter used here is the date column. We can use any column to split up as per need.
  • Third parameter used here is the names of new columns to make.
  • Fourth parameter is the string to split on. Basically this is the separation criteria. By default, separate() will split on any non-alphanumeric characters.

3. Given below is sample input dataset and the code snippet. When we execute the code mentioned in Figure2 using the dataset in Figure1, is the desired output same as input data? Explain with your response.

Figure1 (input dataset)

Assume this data exists in your data frame in R as “my_df”

Figure2 (code snippet)

The output data will not be same as that of input.

Output will look like below.

Col1
Col2
Col3
Col4
AA110100708/11/2002
BB45100908/12/1999
CC65100504/13/2002
DD40101308/14/2002
EE50101001/15/2002
FF45101007/16/2002

The difference is in the format of Col4 which is the date value.
Separate() function splits into 3 different parts of this date column.
Unite() function unites these 3 different parts into one column which is Col4.
However the format is slightly different as mentioned in the code.

Here we are converting non-tidy format to tidy format and again back to non-tidy format.

This is one of the most frequently asked R programming interview questions and answers for freshers in recent times.

4. When to use the following functions: apply(), lapply(), sapply(), tapply() in R? Explain.

The differences are the following:

  • apply(): Use as an alternative to for() loop
  • lapply(): Applies function to every item and returns the result as a list
  • sapply(): function will be executed column wise
  • tapply(): Similar to aggregate() function

5. Are the following code snippets same or different? Explain why to support your response.

These are NOT same. Flights_mutate1 will perform appropriately. Where as

flights_mutate2 will throw an error. We can not use select because the derived variables “speed” does not exist. It has to be created first using mutate() function and then select() function can be used to extract specific variables from the data frame.

Want to Know More?
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Description

R is a programming language at your disposal which can be used for multiple purposes like statistical analysis, predictive modeling, data manipulation, data visualization, etc. It holds a high percentage of market share in the analytics industry. R is an open source programming language which is cross-platform compatible, that is it can run on several operating systems with varied Software/Hardware. Candidates proficient in R programming language are generally paid more than Python and SAS programmers. Work on R language proficiency with a course on R Certification with ML.

The average salary of an R Programmer is $76,487 per year.  Big companies including Facebook, Google, Twitter use R programming language.

If you are determined to ace your next interview as an R programmer, these R interview questions and answers will fast-track your career. To relieve you of the worry and burden of preparation for your upcoming interviews, we have compiled the above list of interview questions for R programming with answers prepared by industry experts. Being well versed with these commonly asked R language interview questions will be your very first step towards a promising career as an R programmer.

Candidates can opt for various options after learning R programming. A few are listed below:

  • R Programmer
  • Data Scientist
  • Data Architect
  • Data Analyst

Candidates who wish to build a career as an R programmer can learn more about R programming from the best programming courses for beginners.

Crack your R interview with ease and confidence!

Recommended Courses

Learners Enrolled For
CTA
Got more questions? We've got answers.
Book Your Free Counselling Session Today.