What is the distribution in R?
R Functions for Probability Distributions. Every distribution that R handles has four functions. There is a root name, for example, the root name for the normal distribution is the norm. This root is prefixed by one of the letters. p for “probability”, the cumulative distribution function (c. d. f.)
What are vector functions?
In R, a function is a piece of code written to carry out a specified task. R Functions are called as objects because we can work with them exactly the same way we work with any other type of object. Vector functions are those functions which we used in vectors.
For Example: rep(), seq(), using all() and any(), more on c() etc.
Most common functions which we use in vector operations are –
rep()
seq()
Explain how to repeat vectors in R?
We can use the rep() function in several ways if we want to repeat the complete vector. For examples: specify the argument times 1. To repeat the vector c(0, 0, 7) three times, use this code:
> rep(c(0, 0, 7), times = 4)
[1] 0 0 7 0 0 7 0 0 7 0 0 7 2
We can also repeat every value by specifying the argument each, like this:
> rep(c(2, 4, 2), each = 2)
[1] 2 2 4 4 2 2 3
We can tell R for each value how often it has to repeat:
> rep(c(0, 7), times = c(4,3))
[1] 0 0 0 0 7 7 7 4
In seq, we use the argument length.out to define R. it will repeat the vector until it reaches that length, even if the last repetition is incomplete.
> rep(1:3,length.out=9)
[1] 1 2 3 1 2 3 1 2 3
How to create vectors in R?
a) To create a vector using integers:
For Example, We use the colon operator (:) in R.
The code 2:6 gives you a vector with the numbers 2 to 6, and 3:-4 create a vector with the numbers 3 to –4, both in steps of 1.
b) We use the seq() to make steps in a sequence.
Seq() function used to describe by which the numbers should decrease or increase.
For Example In R, the vector with a numbers 4.5 to 3.0 in steps of 0.5.
> seq(from = 4.5, to = 3.0, by = -0.5)
[1] 4.5 4.0 3.5 3.0 c
You can specify the length of the sequence by using the argument out. R calculates the step size itself. For Example We can make a vector of nine values going from –2.7 to 1.3 like this:
> seq(from = -2.7, to = 1.3, length.out = 9)
[1] -2.7 -2.2 -1.7 -1.2 -0.7 -0.2 0.3 0.8 1.3
Can we update and delete any of the elements in a list?
We can update any of the element but we can delete only the element at the end of the list.
How many types of object are present In R?
There are 6 types of objects present in R:
Vectors
Matrices
Arrays
Lists
Data Frames
Factors
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.
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().
R Functions for Probability Distributions. Every distribution that R handles has four functions. There is a root name, for example, the root name for the normal distribution is the norm. This root is prefixed by one of the letters. p for “probability”, the cumulative distribution function (c. d. f.)
What are vector functions?
In R, a function is a piece of code written to carry out a specified task. R Functions are called as objects because we can work with them exactly the same way we work with any other type of object. Vector functions are those functions which we used in vectors.
For Example: rep(), seq(), using all() and any(), more on c() etc.
Most common functions which we use in vector operations are –
rep()
seq()
HCL Technologies R Programming Recently Asked Interview Questions Answers |
Explain how to repeat vectors in R?
We can use the rep() function in several ways if we want to repeat the complete vector. For examples: specify the argument times 1. To repeat the vector c(0, 0, 7) three times, use this code:
> rep(c(0, 0, 7), times = 4)
[1] 0 0 7 0 0 7 0 0 7 0 0 7 2
We can also repeat every value by specifying the argument each, like this:
> rep(c(2, 4, 2), each = 2)
[1] 2 2 4 4 2 2 3
We can tell R for each value how often it has to repeat:
> rep(c(0, 7), times = c(4,3))
[1] 0 0 0 0 7 7 7 4
In seq, we use the argument length.out to define R. it will repeat the vector until it reaches that length, even if the last repetition is incomplete.
> rep(1:3,length.out=9)
[1] 1 2 3 1 2 3 1 2 3
How to create vectors in R?
a) To create a vector using integers:
For Example, We use the colon operator (:) in R.
The code 2:6 gives you a vector with the numbers 2 to 6, and 3:-4 create a vector with the numbers 3 to –4, both in steps of 1.
b) We use the seq() to make steps in a sequence.
Seq() function used to describe by which the numbers should decrease or increase.
For Example In R, the vector with a numbers 4.5 to 3.0 in steps of 0.5.
> seq(from = 4.5, to = 3.0, by = -0.5)
[1] 4.5 4.0 3.5 3.0 c
You can specify the length of the sequence by using the argument out. R calculates the step size itself. For Example We can make a vector of nine values going from –2.7 to 1.3 like this:
> seq(from = -2.7, to = 1.3, length.out = 9)
[1] -2.7 -2.2 -1.7 -1.2 -0.7 -0.2 0.3 0.8 1.3
Can we update and delete any of the elements in a list?
We can update any of the element but we can delete only the element at the end of the list.
How many types of object are present In R?
There are 6 types of objects present in R:
Vectors
Matrices
Arrays
Lists
Data Frames
Factors
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.
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().
Post a Comment