August 29, 2018

Sreekanth B

R Programming language Advanced Experienced Level Interview Questions Answers

How to construct a new S3 class?

For Example:

> jim <- list(height = 2.54 * 12 * 6/100, weight = 180/2.2,+ name = "James") > class(jim) <- "person" > class(jim)

We have now made an object of class person
We now define a print method.

> print(jim)
> print.person <- function(x, ...) { + cat("name:", x$name, "\n") + cat("height:", x$height, "meters", "\n") + cat("weight:", x$weight, "kilograms", "\n") + } > print(jim)
Note the method/class has the ”dot” naming convention of method.class.

What is inheritance in S3 class?

In S3, inheritance is achieved by the class attribute being a vector.
For Example:

> fit <- glm(rpois(100, lambda = 1) ~ + 1, family = "poisson" > class(fit)
> methods("residuals")
> methods("model.matrix")
If no method for the first is found, the second class is checked.

What are useful S3 method functions?

methods(“print”) and methods(class = “lm”)
getS3method(“print”,”person”) : Gets the appropriate method associated with a class, useful to see how a method is implemented.
Sometimes, methods are non-visible, because they are hidden in a namespace. Use getS3method or getAnywhere to get around this-
> residuals.HoltWinters
> getS3method("residuals.HoltWinters")
> getAnywhere("residuals.HoltWinters")

How to construct new S4 class?

velocity = c(0.0,0.0)
# Make a function that can test to see if the data is consistent.
# This is not called if you have an initialize function defined!
if(sum(object@velocity^2)>100.0) {
return("The velocity level is out of bounds.")
Now that the code to define the class is given we can create an object whose class is Agent.

> a <- Agent() > a
An object of class "Agent"
Slot "location":
[1] 0 0
Slot "velocity":
[1] 0 0
Slot "active":
[1] TRUE

What is the regular expression in R string manipulation?

A set of strings will define as regular expressions. We use two types of regular expressions in R, extended regular expressions (the default) and Perl-like regular expressions used by perl = TRUE.

What is regular expression syntax?

It specifies characters to seek out, with information about repeats and location within the string. This will practice with the help of metacharacters that have a specific meaning: $ * + . ? [ ] ^ { } | ( ) \

What is the Use of String Utilities in the edtdbg Debugging Tool in R string manipulation?

The internal code of the edtdbg debugging tool makes heavy use of string utilities. A typical example of such usage is the dgbsendeditcmd() function:

# send command to editor
dbgsendeditcmd <- function(cmd) {
syscmd <- paste("vim --remote-send ",cmd," --servername ",vimserver,sep="")
The main point is that edtdbg sends remote commands to the Vim text editor. For instance, if we are running Vim with a server name of 168 and we want the cursor in Vim to move to line 12, we could type this into a terminal (shell) window:

vim –remote-send 12G –server name 168

The effect would be the same as if you had typed.

What is Recursion in R?

calculate_sum() <- function(n) {
if(n <= 1) { return(n) } else { return(n + calculate_sum(n-1)) } } Output: > calculate_sum(4)
[1] 10
Here, calculate_sum(n-1) is been used to compute the addition up to that number.Let suppose the user passes 4 to

What is Recursive Function in R?

Recursive functions call themselves. They break down the problem into the smallest possible components. The function() calls itself within the original function() on each of the smaller components. After this, the results will put together to solve the original problem.

For Example:

 Factorial <- function(N)
if (N == 0)
return( N * Factorial (N-1))

What are applications of Recursion?

a) Dynamic Programming

It is the process of avoiding recomputation. It is an essential tool for statistical programming. There are two types of dynamic programming:

i) Bottom-up dynamic programming

In this, we check function starting with smallest possible argument value.
All computed values will be stored in an array.
ii) Top-down dynamic programming

Save each computed value as the final act of a recursive function.
Check if pre-computed values exist as the first action.
b) Divide-And-Conquer Algorithms

It is a Common class of recursive function.
Common features of this algorithm are process inputs, divide the input into
Smaller portions, Recursive call(s) process at least one part.
Recursion may sometimes occur before the input is been processed.

What are Features of Recursion?

The use of recursion, often, makes code shorter and looks clean.
It is a Simple solution for a few cases.
It expresses in a function that calls itself.

What is OOP in R?

Object-oriented programming is a popular programming language. It allows us to construct modular pieces of code which are used as building blocks for large systems. R is a functional language. It also supports exists for programming in an object-oriented style. OOP is a superb tool to manage complexity in larger programs. It is particularly suited to GUI development. R has two different OOP systems, known as S3 and S4.

What is S3 in R?

The S3 class is used to overload any function. It means calling a different name of the function. It depends upon the type of input parameter or the number of a parameter).

What is S4?

The S4 class is a characteristic OOP system, but it is tricky to debug.

What is reference class?

Reference classes are the modern alternative for S4 classes.

How to create the S3 class?

We can show how to define a function that will create and return an object of a given class. A list is been created with the relevant members, the list’s class is set, and a copy of the list is been returned.

What is S4 Generic?

The function setGeneric is the call to make a new generic function.

> setGeneric("BMI", function(object) standardGeneric("BMI"))
> setMethod("BMI", "personS4", function(object) {
+ object@weight/object@height^2
+ })
> BMI(jimS4)

How to request an input from the user through keyboard and monitor?

In R, there are a series of functions that can be used to request an input from the user,
including readline(), cat(), and scan(). But, I find the readline() function to be the optimal function for this task.

How to read data from the keyboard?

To read the data from keyboard we use three different functions:


How many ways are there to read and write files?

There are three ways to read and write files respectively:

Reading a data or matrix from a file
Reading a single File One Line at a Time
Writing a Table to a File

Explain how to read data or a matrix from a file?

a) Usually we use function read.table() to read data. The default value of a header is ‘FALSE’ and hence when we do not have a header, we need not say such. , R factors are also called as character strings. For turning this “feature” off, you can include the argument in your call to read.table().
b) When you have a spreadsheet export file, i.e. having a type.csv where the fields are divided by commas in place of spaces, use read.csv() in place of read.table(). To read spreadsheet files we can use read.xls.
c) When you read in a matrix using read.table(), the resultant object will become a data frame, even when all the entries got to be numeric. A case exists which may followup call towards as.matrix() in a matrix. We need to read it into a matrix form like this
> x <- matrix(scan(“x”),nrow=5,byrow=T)

What are Sockets in R?

Sockets provide two networked machines with a bidirectional communication channel. Servers are accessed via socket addresses, a combination of the server&#39;s IP address and a port number. We use the port as a connection point on the server, like USB or Firewire ports, with each port serving a specific purpose.

For example:

Web pages are served on port 80 (HTTP), emails are sent via port 25 (SMTP).


make.socket(host = "localhost", port, fail = TRUE, server = FALSE)

host name of remote host
port port to connect to/listen on
fail failure to connect is an error?
Server a server socket?

What is protocol?

A protocol is a set of rules and procedures. It means what format to use, what data mean, when should data be sent. When two computers exchange data, they can understand each other if both follow specific format and rules in a protocol. It is a set of rules and procedures and computers. It is been used to communicate.

What does TCP/IP work?

TCP/IP protocols map to a four-layer conceptual model known as the DARPA model. The four layers of the DARPA model are Application, Transport, Internet, and Network Interface.

Explain TCP/IP applications, services and protocols?

Bootstrap protocol – Bootstrap Protocol (BOOTP) provides a dynamic method for associating workstations with servers. It is a method which also provides a dynamic method for assigning workstation Internet Protocol (IP) addresses and initial program load (IPL) sources.
Domain name system – We use Domain Name System (DNS) to manage host names and their associated Internet Protocol (IP).
Email – Use this information to plan for, configure, use, manage, and troubleshoot e-mail on your system.
Open shortest path first search – IBM I support includes the Open Shortest Path First (OSPF) protocol. OSPF is a link-state, hierarchical Interior Gateway Protocol (IGP) for network routing.
RouteD – The Route Daemon (RouteD) provides support for the Routing Information Protocol (RIP) on the IBM platform.
Simple network time protocol – It is a time-maintenance application that we can use to synchronize hardware in a network.

What is TCP/IP variable SMC-R storage allocations?

Each more RMB that is been allocated for a particular SMC-R link group can accommodate 4 – 32
more TCP connections, depending on the RCVBFRSIZE value of the TCP connections.
More staging buffers are allocated as the volume of application data that is been sent increases.
RMBs, staging buffers, and RDMA receive and send elements are all eligible to be deallocated if the volume of application traffic decreases.

What is debugging in R?

A grammatically correct program may give us incorrect results due to logic errors. In case such errors (i.e. bugs) occur, we need to find out why and where they occur so that you can fix them. The procedure to identify and fix bugs is called “debugging”.

What are tools for debugging in R?

There are five toos present for debugging in R respectively:


What are connections In R?

Functions to Manipulate Connections (Files, URLs, …). Functions to create, open and close connections.
For example: “generalized files”, such as compressed files, URLs, pipes, etc.

file, connection

Explain an Extended example of connections?

Extended Example: Reading PUMS sample files
These files contain records for a sample of housing units with information on the characteristics of each unit.

 Why use PUMS?

PUMS files provide greater accessibility to inexpensive data for research projects. Thus it is beneficial for students as they are looking for greater accessibility to inexpensive data. Social scientists often use the PUMS for regression analysis and modeling applications.

How can I access PUMS?

Statistical software is the tool used to work with PUMS files.

Which function is used to write files?

We use write.csv() to write files.

Explain how to write files?

We use write.csv() to write file. By default, write.csv() includes row names.

# A sample data frame
data <- read.table(header=TRUE, text='
subject sex size
1 M 7
2 F NA
3 F 9
4 M 11
# Write to a file, suppress row names
write.csv(data, "data.csv", row.names=FALSE)
# Same, except that instead of "NA", output blank cells
write.csv(data, "data.csv", row.names=FALSE, na=" ")
# Use tabs, suppress row names and column names
write.table(data, "data.csv", sep="\t", row.names=FALSE, col.names=FALSE)

What are fundamental principles of debugging?

Programmers often find that they spend more time debugging a program than actually writing it. Good debugging skills are invaluable. There are four basic principles of debugging:

The essence of debugging: The principle of confirmation
Start Small
Debug in a Modular, Top-Down Manner

What is a Graphic device?

A graphics device is something where we can make a plot to appear. When we make a plot in R, it has to be “sent” to a specific.

Window on your computer (screen device) 2. PDF file (file device)
PNG or JPEG file (file device)
Scalable vector graphics (SVG) file (file device)
The most common place for a plot to be “sent” is the screen device.

A Mac, the screen device is launched with the quartz()
Windows, the screen device is launched with windows()
Unix/Linux, the screen device is launched with x11()

Explain R graphics devices?

The following devices are currently available:

PDF – Write PDF graphics commands to file.
xfig – Device for XFIG graphics file format.
pictex – Writes TeX/PicTeX graphics commands to a file (of historical interest only).
postscript – Writes PostScript graphics commands to a file.
bitmap – bitmap pseudo-device via Ghostscript (if available).
The following devices will be functional if R was compiled to use them:

png – PNG bitmap device
jpeg – JPEG bitmap device
bmp – BMP bitmap device
tiff – TIFF bitmap device
X11 – The graphics device for the X11 windowing system.
svg – SVG device based on Cairo graphics.
cario.pdf – cairo_ps PDF and PostScript devices based on Cairo graphics.
quartz – The graphics device for the macOS native Quartz 2d graphics system.

Explain how to save graphs in R?

R runs on so many different operating systems. It supports so many different graphics formats.

JPEG – jpeg is been used anywhere but doesn’t resize
PNG – png is been used anywhere but doesn’t resize
WMF – win.metafile Windows only; best choice with Word; easily resizable
PDF – pdf pdflatex; easily resizable
Postscript – Postscript latex and Open Office; easily resizable

How many methods are there to save graphs?

There are three methods to save graphs respectively:

A General method
another approach
Local Sessions with Windows or OS X

How Using Rprof() to Find Slow Spots in Your Code in R?

If our R code is running unnecessarily slowly, a handy tool for finding the

a) Monitoring with Rprof() – We will call Rprof() to get the monitor started, run our code, and then call Rprof() with a NULL argument to stop the monitoring.
b) Profiling R code – Profiling R code gives you the chance to identify bottlenecks and pieces of code that needs to be more efficiently implemented just by changing one line of the code from

x = data.frame(a = variable1, b = variable2)


x = c(variable1, variable2)

This big reduction happened because this line of code was called several times during the execution of the function.
c) Using R code profiling functions

rprof is a function included in the base package utils, which is loaded by default.
To use R profiling in our code, we can call this function and specify its parameters, including the name of the location of the log file that will be written. See the help for rprof for details.
Profiling can be turned on and off in your code.

What is TCP/IP in R?

TCP/IP is a set of protocols. It is a primary tech of the internet. When we browse the web, send email, chat online, online gaming, TCP/IP is working underneath.

What is TCP/IP variable SMC-R storage allocations?

Each more RMB that is been allocated for a particular SMC-R link group can accommodate 4 – 32
more TCP connections, depending on the RCVBFRSIZE value of the TCP connections.
More staging buffers are allocated as the volume of application data that is been sent increases.
RMBs, staging buffers, and RDMA receive and send elements are all eligible to be deallocated if the volume of application traffic decreases.

What is vectorization in R?

Vectorized functions are a very useful feature of R, but programmers who are used to other languages often have trouble with this concept at first. A vectorized function works not just on a single value, but on a whole vector of values at the same time. But if you understand the nuts and bolts of vectorization in R, it may help you write shorter, simpler, safer, and yes, the faster code in the first place.

What is bytecode compilation?

Bytecode objects consist of an integer vector representing instruction opcodes and operands, and a generic vector representing a constant pool. The compiler is implemented almost entirely in R, with just a few support routines in C to manage compiled code objects is called compiler interface.

What is the byte in R?

A byte is a data equal to either seven or eight bits depending if it needs error correction (parity)

What is JIT in R?

There are two R packages available that offers Just-in-time compilation to R users: the {jit} package and the {compiler} package.

What is JIT package?

The {jit} package was created by Stephen Milborrow which provides a just-in-time compilation of R loops and arithmetic expressions in loops, enabling such code to run much faster.

What is Preliminaries in R?

R is case sensitive
# is the comment tag
R is installed with a default library/packages. We can add/ import extra Packages to the library using the command library()
To use a library function you must load it first into memory using the command load()
Basic instructions are memory resident (you do not need to load them)
Variable names cannot start with “.” (dot), “+” (plus sign) or “-”(minus sign)

How many types of C/C++ preliminaries are present in R?

There are so many types of C/C++ preliminaries are present in R. some of them are mentioned below:

Basic datatype
User-defined datatype
Derived datatype

Explain in brief preliminaries of C/C++?

a) Tokens – The smallest individual units in a program are known as tokens. It has the following tokens:


We can write a program in a C++ using tokens, white spaces and the syntax of the language.

i) Keywords – The keyword implements specific C++ language features. They have explicitly reserved identifiers and can’t be used as names for the program variables or other user-defined program elements. Some of the C++ keywords are illustrated; which are in addition to set of ANSI C keywords.


ii) Identifiers – Identifiers refer to the names of variables, functions, arrays, classes etc. created by programmers. These are the fundamental need of any language. Each language has its own rules for naming these identifiers. The following rules are common : ( both in C, C++ )

Only alphabetic characters, digits and underscores are permitted.
The name can’t start with a digit.
Uppercase and lowercase letters are distinct.
A declared keyword can’t be used as a variable name. However major difference lies in the limit of the length of a name.
b) Basic datatype
C++ compilers support most of the basic data type supported by ANSI C compilers
Char, float, Int, char, double, void. The purpose of the data type void is contrasting. These include

specifications of the return type of a function not returning any value.
to indicate an empty argument list to a function.
The introduction of generic pointers. E.g. void *gp;
c) User-defined datatype
We use user-defined data type structure & union in C while C++ also permits to define another user-defined data type known as class, that can be used to declare variables

d) Derived data type

Arrays – In C++ the application of arrays is like that of C only exception being the way the character array is initialized.
Functions – The function has undergone major change over in C++. While some of this changes are simple, others just a new way of thinking when organizing our program.Some of these were introduced to make C++ program more readable & reliable
Pointers – Pointers are declared & initialized as in C
int x;
int *pi;
pi=&x; etc.

C++ adds the concept of constant pointer & pointer to constant.
e.g. char *const ptr1 = “NEW” // constant pointer

What does the term”Dreaded for loop” means?

In R, many questions arise how to accomplish various tasks without for loops. There seems to be a feeling that programmers should avoid these loops at all costs.Those who pose the queries usually have the goal of speeding up their code.

Give an example of “Dreaded for loop”?

Ex- Vectorization for speedup

Sometimes, we can use vectorization instead of looping. For example, if x and y are vectors of equal lengths, you can write this:
z <- x + y
This is not only more compact, but even more important, it is faster than using this loop and if we understand the nuts and bolts of vectorization in R, it may help us to write shorter, simpler, safer, and yes, a faster code in the first place.

What Is functional programming and memory issues in on the performance basis?

Functional Programming – R is a functional programming (FP) language. This means that it provides many tools for the creation and manipulation of functions. In particular, R has what’s known as first-class functions.
Memory issues – Most R objects are immutable, or unchangeable. Thus, R operations are implemented as functions that re-assign to the given object, a trait that can have performance implications.

What do copy-on-change issues in R?

We will discuss an important feature of R that makes it safer to work with data. Suppose we create a simple numeric vector x1:

x1 <- c(1, 2, 3)

Then, we assign the value of x1 to x2:

x2 <- x1

Now, x1 and x2 have exactly the same value. What if we modify an element in one of the two vectors? Will both vectors change?

x1[1] <- 0
## [1] 0 2 3
## [1] 1 2 3
The output shows that when x1 is changed, x2 will remain unchanged. You may guess that the assignment automatically copies the value and makes the new variable point to the copy of the data instead of the original data.

How to compile and run code in R?

Compiled Code – R is high-level, expressive language.While C and C++ often require more lines of code to solve the same problem, they can be orders of magnitude faster than R.
Run the code – We find it easiest to use Rstudio. We can also paste the code in a normal R console or let R run a source file. However, these approaches are a bit less fail-safe.

What are debuggers and debugging techniques in R?

To complete a programming project writing code is only the beginning. After the original implementation is complete, it is time to test the program.Hence, debugging takes on great importance: the earlier you find an error, the less it will cost. A debugger allows us, the programmer, to interact and inspect the running program, making it possible to trace the flow of execution and track down the problems.

a) G.D.B. – It is the standard debugger for Linux and Unix-like operating systems.

b) Static Analysis – Searching for errors using psv studio- An introduction to analyzing code to find potential errors via static analysis, using the PVS-Studio tool.
c) Advanced Linux debugging

Haunting segmentation faults and pointing errors- Learn how to debug the trickiest programming problems
finding memory leaks and other errors with Valgrind- Learn how to use Valgrind, a powerful tool that helps find memory leaks and invalid memory usage.
Visual Studio- Visual Studio is a powerful editor and debugger for Windows

How to use R from python?

Calling R from python- First of all, we have to have both R and python installed. we then need to install rpy2. PANDAS recommends that we download version 2.2.x but I have used 2.3.0 without any difficulties. This is not a description of how to use R. This is presented for those that already know R and want to call it from within python to use the advanced PANDAs data manipulation tools.

What is string manipulation in R?

Generic programming in an OpenCL program was restricted to using a string manipulation mechanism, where the program was constructed as a string at runtime and then passed to the OpenCL driver fronted, that will finally compile and build the kernel at runtime. Command group that call kernels can also be templated, allowing for a complex position of functors and types.

How many types of functions are there in R string manipulation?

There are 8 functions in R string manipulation respectively


Subscribe to get more Posts :