March 23, 2019

Srikaanth

RELX Group R Programming Recently Asked Interview Questions Answers

How to call C function from R?

Generally, to call a C function it required two pieces:

C function.
R wrapper function that uses.Call().
The function below adds two numbers together:

// In C -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#include <R.h>
#include <Rinternals.h>
SEXP add(SEXP a, SEXP b) {
SEXP result = PROTECT(allocVector(REALSXP, 1));
REAL(result)[0] = asReal(a) + asReal(b);
UNPROTECT(1);
return result;
}
# In R -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
add <- function(a, b) {
.Call("add", a, b}
}
RELX Group R Programming Recently Asked Interview Questions Answers
RELX Group R Programming Recently Asked Interview Questions Answers

What are R matrices and R matrices functions?

A matrix is a two-dimensional rectangular data set. Thus it can create using vector input to the matrix function. Also, a matrix is a collection of numbers arranged into a fixed number of rows and columns. Usually, the numbers are the real numbers. We reproduce a memory representation of the matrix in R with the matrix function. Hence, the data elements must be of the same basic type. Matrices functions are those functions which we use in matrices.
There are two types of matrices functions:

apply()
sapply()

What is apply() function in R?

Return a vector or array or list of values obtained by applying a function to margins of an array or matrix.

Keywords
array, iteration

Usage

apply(X, MARGIN, FUN, …)

Arguments

X – an array, including a matrix
… – optional arguments to FUN
FUN – The function to apply: see ‘Details’
MARGIN -Functions will apply on subscripts in a vector.

What is the apply() family in R?

Apply functions are a family of functions in base R. which allow us to perform an action on many chunks of data. An apply function is a loop, but run faster than loops and often must less code. There are many different apply functions. The called function could be:

There is some aggregating function. They include meaning, or the sum(includes return a number or scalar);
Other transforming or subsetting functions.
There are some vectorized functions. They return more complex structures like lists, vectors, matrices, and arrays.
We can perform operations with very few lines of code in apply().

What is sapply() in R?

A Dimension Preserving Variant of “sapply” and “lapply”
sapply is a user-friendly version. It is a wrapper of lapply. By default sapply returning a vector, matrix or an array.

Keywords
Misc, utilities

Usage

Sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
Lapply(X, FUN, ...)
Arguments

X – It is a vector or list to call sapply.
FUN – a function.
… – optional arguments to FUN.
simplify – It is a logical value which defines whether a result is been simplified to a vector or
matrix if possible?
USE.NAMES – logical; if TRUE and if X is a character, use X as names for the result unless it had names already.

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!
validity=function(object)
{
if(sum(object@velocity^2)>100.0) {
return("The velocity level is out of bounds.")
}
return(TRUE)
}
)
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="")
system(syscmd)
}
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(1)
else
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.


Subscribe to get more Posts :