September 24, 2018

Sreekanth B

Infosys R Programming Recently Asked Interview Questions Answers


What are R Functions?

A function is a piece of code written to carry out a specified task. Thus it can or can’t accept arguments or parameters and it can or can’t return one or more values. In R, functions are objects in their own right. Hence, we can work with them exactly the same way we work with any other type of object.

What is using all() and any()?

a) na.rm – State whether NA values should ignore.

b) any(…, na.rm=FALSE) … – One or more R objects that need to be check. na.rm – State whether NA values should ignore. The any() and all() functions are shortcuts because they report any or all their arguments are TRUE.

> x <- 1:10 > any(x > 5)
[1] TRUE
> any(x > 88)
[1] FALSE
> all(x > 88)
[1] FALSE
> all(x > 0)
[1] TRUE For Example: Suppose that R executes the following:
> any(x > 5)
It first evaluates x > 5:

(FALSE, FALSE, FALSE, FALSE, FALSE)

We use any() function – that reports whether any of those values are TRUE while all() function works and
reports if all the values are TRUE.
Infosys R Programming Recently Asked Interview Questions Answers
Infosys R Programming Recently Asked Interview Questions Answers

What is R’s C interface?

R’s source code is a powerful technique for Improving Programming skills. But, many base R function was already written in C. It is been used to figure out how those functions work. All functions in R defined with the prefix Rf_ or R_.

Outline of Rs C interface

Input Validations talks about itself so that C function doesn’t crash R.
C data Structures shows how to translate data structure names from R to C.
Creating and modifying vectors teaches how to create, change, and make vectors in C.
Calling C defines the basics of creating. It also defines the functions with the inline package.

What are Prerequisites for R’s C interface?

We need a C compiler for C interface. Windows users can use Rtools. Mac users will need the Xcode command line tools. Most Linux distributions will come with the necessary compilers. In Windows, it is necessary to include Windows PATH environment variable in it:

Rtools executables directory (C:\Rtools\bin),
C compiler executables directory (C:\Rtools\gcc-4.6.3\bin).

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}
}

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.

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:

scan()
readline()
print().


Subscribe to get more Posts :