R – Questions

What is R Programming?

R is a programming language meant for statistical analysis and creating graphs for this purpose.Instead of data types, it has data objects which are used for calculations. It is used in the fields of data mining, Regression analysis, Probability estimation etc., using many packages available in it.

What are the different data objects in R?

There are 6 data objects in R. They are vectors, lists, arrays, matrices, data frames and tables.

What makes a valid variable name in R?

A valid variable name consists of letters, numbers and the dot or underline characters. The variable name starts with a letter or the dot not followed by a number.

What is the main difference between an Array and a matrix?

A matrix is always two dimensional as it has only rows and columns. But an array can be of any number of dimensions and each dimension is a matrix. For example a 3x3x2 array represents 2 matrices each of dimension 3×3.

Which data object in R is used to store and process categorical data?

The Factor data objects in R are used to store and process categorical data in R.

How can you load and use csv file in R?

A csv file can be loaded using the read.csv function. R creates a data frame on reading the csv files using this function.

How do you get the name of the current working directory in R?

The command getwd() gives the current working directory in the R environment.

What is R Base package?

This is the package which is loaded by default when R environment is set. It provides the basic functionalities like input/output, arithmetic calculations etc. in the R environment.

How R is used in logistic regression?

Logistic regression deals with measuring the probability of a binary response variable. In R the function glm() is used to create the logistic regression.

How do you access the element in the 2nd column and 4th row of a matrix named M?

The expression M[4,2] gives the element at 4th row and 2nd column.

What is recycling of elements in a vector? Give an example.

When two vectors of different length are involved in a operation then the elements of the shorter vector are reused to complete the operation. This is called element recycling. Example – v1 <- c(4,1,0,6) and V2 <- c(2,4) then v1*v2 gives (8,4,0,24). The elements 2 and 4 are repeated.

What are different ways to call a function in R?

We can call a function in R in 3 ways. First method is to call by using position of the arguments. Second method id to call by using the name of the arguments and the third method is to call by default arguments.

What is lazy function evaluation in R?

The lazy evaluation of a function means, the argument is evaluated only if it is used inside the body of the function. If there is no reference to the argument in the body of the function then it is simply ignored.

How do you install a package in R?

To install a package in R we use the below command.

install.packages("package Name")
Name a R packages which is used to read XML files.

The package named “XML” is used to read and process the XML files.

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.

Give the general expression to create a matrix in R.

The general expression to create a matrix in R is – matrix(data, nrow, ncol, byrow, dimnames)

which function is used to create a boxplot graph in R?

The boxplot() function is used to create boxplots in R. It takes a formula and a data frame as inputs to create the boxplots.

In doing time series analysis, what does frequency = 6 means in the ts() function?

Frequency 6 indicates the time interval for the time series data is every 10 minutes of an hour.

What is reshaping of data in R?

In R the data objects can be converted from one form to another. For example we can create a data frame by merging many lists. This involves a series of R commands to bring the data into the new format. This is called data reshaping.

What is the output of runif(4)?

It generates 4 random numbers between 0 and 1.

How to get a list of all the packages installed in R ?

Use the command

installed.packages()
What is expected from running the command – strsplit(x,”e”)?

It splits the strings in vector x into substrings at the position of letter e.

Give a R script to extract all the unique words in uppercase from the string – “The quick brown fox jumps over the lazy dog”.

x <- "The quick brown fox jumps over the lazy dog"
split.string <- strsplit(x, " ")
extract.words <- split.string[[1]]
result <- unique(tolower(extract.words))
print(result)
Vector v is c(1,2,3,4) and list x is list(5:8), what is the output of v*x[1]?

Error in v * x[1] : non-numeric argument to binary operator

Vector v is c(1,2,3,4) and list x is list(5:8), what is the output of v*x[[1]]?

[1] 5 12 21 32s

What does unlist() do?

It converts a list to a vector.

Give the R expression to get 26 or less heads from a 51 tosses of a coin using pbinom.

x <- pbinom(26,51,0.5)
print(x)
X is the vector c(5,9.2,3,8.51,NA), What is the output of mean(x)?

NA

How do you convert the data in a JSON file to a data frame?

Using the function as.data.frame()

Give a function in R that replaces all missing values of a vector x with the sum of elements of that vector?

function(x) { x[is.na(x)] <- sum(x, na.rm = TRUE); x }
What is the use of apply() in R?

It is used to apply the same function to each of the elements in an Array. For example finding the mean of the rows in every row.

Is an array a matrix or a matrix an array?

Every matrix can be called an array but not the reverse. Matrix is always two dimensional but array can be of any dimension.

How to find the help page on missing values?

?NA

How do you get the standard deviation for a vector x?

sd(x, na.rm=TRUE)

How do you set the path for current working directory in R?

setwd(“Path”)

What is the difference between “%%” and “%/%”?

“%%” gives remainder of the division of first vector with second while “%/%” gives the quotient of the division of first vector with second.

What does col.max(x) do?

Find the column has the maximum value for each row.

Give the command to create a histogram.
hist()
How do you remove a vector from the R workspace?

rm(x)

List the data sets available in package “MASS”

data(package = “MASS”)

List the data sets available in all available packages.

data(package = .packages(all.available = TRUE))
What is the use of the command – install.packages(file.choose(), repos=NULL)?

It is used to install a r package from local directory by browsing and selecting the file.

Give the command to check if the element 15 is present in vector x.

15 %in% x
Give the syntax for creating scatterplot matrices.

pairs(formula, data)

Where formula represents the series of variables used in pairs and data represents the data set from which the variables will be taken.

What is the difference between subset() function and sample() function in R?

The subset() functions is used to select variables and observations. The sample() function is used to choose a random sample of size n from a dataset.

How do you check if “m” is a matrix data object in R?

is.matrix(m) should retrun TRUE.

What is the output for the below expression all(NA==NA)?

[1] NA

How to obtain the transpose of a matrix in R?

The function t() is used for transposing a matrix. Example – t(m) , where m is a matrix.

What is the use of “next” statement in R?

The “next” statement in R programming language is useful when we want to skip the current iteration of a loop without terminating it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s