Interview Questions about R Language

The post is about Interview Questions about R Language. It contains some basic questions that are usually asked in interviews.

What is R?

R is a programming language and environment for statistical computing and graphics. It is an open-source language that provides a wide variety of statistical and graphical techniques and is highly extensible. The strength of R is the ease with which well-designed publication-quality plots can be produced, including mathematical/statistical symbols and formulae where needed.

Learn R Language and FAQS, Interview Questions about R Language

R language is available as Free Software under the terms of the Free Software Foundation’s GNU General Public License in source code form. It compiles and runs on a wide variety of UNIX platforms and similar systems (including FreeBSD and Linux), Windows, and Mac OS. The R command line interface (CLI) consists of a prompt, usually the > character. Data miners use it for developing statistical software and data analysis.

What is CLI in R?

CLI stands for Command Line Interface. In a command line interface, the user types the command that they want to execute and presses the Return key. For example, if you type the line 2+2 and press the return key, R will give you the result [1] 4.

Interview Questions about R Language

What is GUI in R?

GUI stands for Graphical User Interface. R Language is a command line-driven program. The user enters instructions at the command prompt ( > by default ) and each command is executed one at a time. There have been a number of attempts to create a more graphical interface, ranging from code editors that interact with R, to full-blown GUIs that present the user with menus and dialog boxes.

Who is the Creator of R Language?

R language was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand. It is currently developed by the R Development Core Team, of which Chambers is a member. R is named partly after the first names of the first two R authors and partly as a play on the name of S. The project was conceived in 1992, with an initial version released in 1995 and a stable beta version in 2000.

What are the Applications of the R Language?

  • Many data analysts and researchers use R because R language is the most prevalent language. Hence, R is used as a fundamental tool for data analysis in various disciplines such as mathematics, economics, social sciences, natural sciences, technology and engineering, business and finance, etc.
  • Many quantitative analysts use R as their programming tool. R helps in data importing and cleaning, depending on what manner of strategy the researchers are using.
  • R is best for data Science because it gives a broad variety of statistics and data manipulation tools. In addition, R provides the environment for statistical computing and design. Rather R is considered as an alternate execution of S.

Why R is Important?

R language is a programming language and a leading tool for machine learning, artificial intelligence, data mining, natural language processing, statistics, and data analysis. By using R one can create objects, functions, and packages. R language is a platform independent, so one can use it on any operating system. The downloading and installation of R language is free, therefore, one can use it without purchasing a license.

R is open-source which means anyone can examine the source code to see what exactly is doing on screen. Anyone can add a feature and fix bugs without waiting for the vendor to do this. It also allows the user to integrate with other languages (such as C and C++). It also enables the user to interact with many data sources and statistical packages (such as SAS and SPSS). R has a large growing community of users working day by day to enhance its working and powers.

General Knowledge Quiz, MCQS Statistics with Answers

Packages in R Language

Packages in R Language store all the functions, datasets, and help files that significantly expand the language’s functionality beyond its core capabilities. When a package is loaded, its contents are available to work with. It makes the packages more efficient (as the full list takes more memory and time to search than a subset). The packages are also protected from name clashes with other codes.

Why Use R Packages?

  • Specialized Functionality: R packages offer tailored solutions for various domains, such as,
    • Biostatistics
    • Data mining
    • Machine learning
    • Financial analysis
    • Geospatial analysis
    • Text mining
  • Efficient Code and Algorithms: Many packages incorporate highly optimized C or C++ code, boosting performance and enabling complex computations.
  • Community-Driven Innovation: The R community actively develops and shares packages, ensuring a constant stream of new tools and techniques.
  • Standardized Data Formats: R Packages often include standard data formats, making it easier to work with diverse data sources.
  • Reproducibility: By using R packages, one can share his/her code and analyses more easily, making them reproducible for others

Seeing the Installed Packages

To see what packages are installed in your computer system, use the following command without arguments.

library()

To load particular packages in R (for example, mctest (https://CRAN.R-project.org/package=mctest) package containing functions to compute the multicollinearity diagnostics), use the command like:

library(mctest)
Packages in R Language

Installing and Updating Packages in R

One can install an R package if a system is connected to the internet using install.packages(). A package can also be updated by using the update.packages() command. (The installation of a package is also available through the Packages menu in the Windows and OS X GUIs.

# Installing a package
install.packages("mctest")

# Install Multiple Packages in R
install.packages(c("mctest", "lmridge", "liureg"))

# Updating a package
update.packages("mctest")

Currently Loaded Packages

One can see the packages that are currently loaded in the more by using the command

search()

Note that some packages may be loaded but not available on the search list, such packages may be seen by using

loadedNamespaces()
Packages in R

One can see a list of all available help topics in an installed package, by using the command

help.start()

An HTML help system will start. One can easily navigate to the package listing in the reference section.

Help System in R

Standard/ Base Packages in R

The base or standard packages are considered part of the R source code. The base packages contain the basic functions that allow R to work, and the datasets, standard statistical, and graphical functions that are described in this manual. These packages are automatically available in any R installation.

Contributed Packages and CRAN

There are thousands of contributed/ customized/ user-defined packages for R, written by many different authors. Some of these packages implement specialized statistical methods, some give access to data or hardware, and others are designed to complement textbooks. Most of the R packages are available for download from CRAN (https://CRAN.R-project.org/ and its mirrors).

Key R Package Repositories

  • CRAN: The primary repository for R packages, offering a vast array of options.
  • Bioconductor: Specializes in bioinformatics and computational biology tools.
  • GitHub: Hosts user-contributed packages and open-source projects.

Commonly Used Packages

  • Data Manipulation:
    • dplyr: For data manipulation and transformation.
    • tidyr: For tidying data.
  • Data Visualization:
    • ggplot2: For creating elegant and customizable plots.
    • plotly: For interactive visualizations.
  • Statistical Computing:
    • stats: The Base R package for statistical computations.
    • MASS: For more advanced statistical methods.
  • Machine Learning:
    • caret: For a unified interface to various machine learning algorithms.
    • randomForest: For random forest models.
    • xgboost: For gradient boosting machines.
  • Text Mining:
    • tidytext: For text mining and analysis.
  • Web Scraping:
    • rvest: For extracting data from websites.

Data Analysis and Statistics

Exploring Data Distribution in R

Exploring Data Distribution in R Language

Suppose we have univariate data and need to examine its distribution. There are a variety of tools and techniques to explore univariate data distributions. The simplest way is to explore the numbers. The summary() and fivenum() are numerical while the stem() is a display of the numbers to examine the distribution of the data set. This post will teach you the basics of exploring data distribution in the R Language.

Five Number Summary and Stem and Leaf Plot

One can use numeric and visual tools in exploring data distribution. For example,

attach(faithful)
summary(eruptions)

## Output
 Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.600   2.163   4.000   3.488   4.454   5.100 

fivenum(eruptions)

## Output
 1.6000 2.1585 4.0000 4.4585 5.1000

stem(eruptions)
Exploring Data Distribution in R Language stem and leaf display

Histogram and Density Plot

The stem-and-leaf display is like a histogram which can be drawn using the hist() function to plot histograms in R language. The boxplot() function can also be used to visualize the distribution of the data. This will help in exploring data distribution.

# make the bins smaller, and make a plot of density

hist(eruptions)
hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE)
lines(density(eruptions, bw=0.1))
rug(eruptions) # Show the actual data points
Exploring data distribution in R using hist and density function

The density can be used to create more elegant density plots, a line is also produced by the density and bw bandwidth is chosen by trial and error as the defaults give too much smoothing (it usually does for “interesting” densities). Better automated methods for bandwidth are also available (in the above example bw="SJ" gives good results.)

Empirical Cumulative Distribution Function

One can also plot the empirical cumulative distribution function by using the function ecdf.

plot(ecdf(eruptions), do.points = FALSE, verticals = TRUE)
cdf in R language

For the right-hand mode (eruptions of longer than 3 minutes), let us fit a normal distribution and overlay the fitted CDF.

long <- eruptions[eruptions > 3]
plot (ecdf(long), do.points = FALSE, verticals = TRUE)
x <- seq(3, 5.4, 0.01)
lines(x, pnorm(x, mean = mean(long), sd = sqrt(var(long))), lty = 3)
cdf and normality plot in R
par(pty = "s")
qqnorm(long)
qqline(long)
Normal qq plot

The Quantile-Quantile (QQ Plot) long shows a reasonable fit but a shorter right tail than one would expect from a normal distribution. One can compare it with some simulated data from t-distribution.

x <- rt(250, df = 5)
qqnorm(x)
qqline(x)

which will show a longer tail (as a random sample from the t distribution) compared to a normal distribution.

normal qq plot in r for longer tails

Normality Test in R

To determine if the data follows the normal distribution,

    Shapiro-Wilk normality test
shapiro.test(eruptions)
## Output
		Shapiro-Wilk normality test

data:  eruptions
W = 0.84592, p-value = 9.036e-16

The Kolmogorov-Smirnov Test using the ks.test() function can determine if the data follows a normal distribution

ks.test(eruptions, "pnorm")

## Output
        Asymptotic one-sample Kolmogorov-Smirnov test

data:  eruptions
D = 0.94857, p-value < 2.2e-16
alternative hypothesis: two-sided

Warning message:
In ks.test.default(eruptions, "pnorm") :
  ties should not be present for the one-sample Kolmogorov-Smirnov test

By combining the above techniques, exploring data distribution helps in gaining valuable insights into the distribution of univariate data, identifying potential outliers, and assessing normality assumptions for further statistical analysis.

Online Quiz Website, Learn Basic Statistics