June 1, 2019

Srikaanth

CGI Group R Programming Interview Questions

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.
CGI Group R Programming Recently Asked Interview Questions Answers
CGI Group R Programming Recently Asked 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!
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()

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 as.is=T 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).

Usage

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

Arguments
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:

traceback()
debug()
browser()
trace()                                                                                                                             
recover()


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.

Keywords
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
Antibugging.


Subscribe to get more Posts :