TCS R Programming Recently Asked Interview Questions Answers

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
x1
## [1] 0 2 3
x2
## [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.
TCS R Programming Recently Asked Interview Questions Answers
TCS R Programming Recently Asked Interview Questions Answers

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

grep()
nchar()
paste()
sprintf()
substr()
strsplit()
regex()
gregexpr().


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.


Post a Comment

Previous Post Next Post