Question 1: What is R Programming?
- R Programming language was created by Ross Ihaka and Robert Gentleman, at the University of Auckland.
- R Programming language is generally used for developing statistical analysis, graphics representation, and reporting.
- Now, R is developed by the R Development Core Team.
Question 2: What are the different data objects in R?
Data Objects in R are listed below:
- vectors
- lists
- arrays
- matrices
- data frames
- tables
Question 3: What makes a variable name valid in R?
- Variable name starts with an alphabet letter
- Variable consisting of number, dot, and underscore
Question 4: What is the main difference between an array and a matrix?
Matrix |
Array |
Matrix can be only two dimensional |
Array can be multi-dimensional |
Matrix has rows and columns |
Array can be represented by multiple matrices |
Question 5: Which data object in R is used to store and process categorical data?
In R Programming, factor data objects are used to store and process categorical data.
Question 6: How can you load and use CSV files in R?
In R Programming language, read.cvs method is used to load CSV files.
Question 7: How do you get the name of the current working directory in R?
In R Programming, getwd() method is used to get the current working directory.
Question 8: What is the R Base package?
- R Base package basically provides input/output functionality and arithmetic calculation functionality.
- R Base package is automatically installed at the time of the R Programming Environment setup.
Question 9: How is R used in logistic regression?
In R Programming, glm() method is used to create logistic regression and logistic regression works with measuring the probability of a binary response variable.
Question 10: How do you access the element in the 3rd column and 1st row of a matrix named M?
We can print 1st row and 3rd column using the command print(m[1,3])
Example
- M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
- print(M[1,3])
Output
Question 11: What is the recycling of elements in a vector? Give an example.
In R Programming language, recycling of elements is when we perform operations on two different vectors having different lengths. In it, the elements of the shorter length vector are used to complete the operation.
- v1 <- c(4,1,0,6)
- v2 <- c(2,4)
- print(v1*v2)
Output
Question 12: What are different ways to call a function in R?
- Calling a function with argument values (by position and by name)
Example
-
- new.fun <- function(x,y,z) {
- res <- x * y + z
- print(res)
- }
-
-
- new.fun(5,3,11)
-
-
- new.fun(x = 11, y = 5, z = 3)
Output
- Calling a function with default argument
Example
-
- new.fun <- function(a = 10, b = 20) {
- res <- a * b
- print(res)
- }
-
-
- new.fun()
-
-
- new.fun(9,5)
Output
- Calling a function without an argument
Example
-
- new.fun <- function() {
- for(j in 1:5) {
- print(j^2)
- }
- }
-
-
- new.fun()
Output
Question 13: What is a lazy function evaluation in R?
Argument of function is only executed when they are needed.
Example
-
- new.fun <- function(x, y) {
- print(x^2)
- print(x)
- print(y)
- }
-
-
- new.fun(20)
Output
Question 14: How do you install a package in R?
In R programming, the package is installed using the following command.
- install.packages("package Name")
Question 15:
Name an R package that is used to read XML files?
XML package is used to read XML files.
Example
-
- library("XML")
-
-
- library("methods")
-
-
- res <- xmlParse(file = "input.xml")
-
-
- print(res)
Output
Question 16:
How can we update and delete any of the elements in a list?
In R Programming language, an element is deleted from the last of the list but we can update any element in the list.
Example:
- list1 <- list(c("c++","c#","asp.net"), matrix(c(1,2,3,4,5,6), nrow = 2),
- list("red",20.3))
-
-
- names(list1) <- c("R", "c# corner", ".net")
-
- list1[4] <- "R programming"
- print(list1[4])
-
-
- list1[4] <- NULL
-
-
- print(list1[4])
-
-
- list1[3] <- "R programming language"
- print(list1[3])
Output
Question 17:
Give the general expression to create a matrix in R.
Syntax of matrix creation
: matrix(data, nrow, ncol, byrow, dimnames)
Example
-
- M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
- print(M)
-
-
- N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
- print(N)
-
-
- rownames = c("row1", "row2", "row3", "row4")
- colnames = c("col1", "col2", "col3")
-
- P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
- print(P)
Output
Question 18: Which function is used to create a boxplot graph in R?
In R Programming, Boxplot method is used to create boxplot graph.
Example
-
- png(file = "boxplot.png")
-
-
- boxplot(mpg ~ cyl, data = mtcars, xlab = "Number of Cylinders",
- ylab = "Miles Per Gallon", main = "Data")
-
-
- dev.off()
Output
Question 19:
What is reshaping of data in R?
In R Programming, reshaping of data is the conversion of one data object into other data objects.
Question 20:
What is the output of runif(4)?
runif() method return random number (0-4)
Example
Question 21:
How to get a list of all the packages installed in R ?
In R Programming, we install the list package using the following command.
Question 22:
What is expected from running the command - strsplit(x,"e")?
This method splits string in vector x into a substring at the position of substring e.
Question 23:
Vector v is c(1,2,3,4) and list x is list(5:8), what is the output of v*x[1]?
Error: non-numeric argument.
Question 24:
What does unlist() do?
This function converts a list into a vector.
Question 25:
Give the R expression to get 26 or fewer heads from 51 tosses of a coin using pbinom.?
Example
- x <- pbinom(26,51,0.5)
- print(x)
Output
Question 26:
How do you convert the data in a JSON file to a data frame?
In R Programming, data in a JSON file can be converted using as.data.frame() method.
You can enhance your knowledge more, by reading the following articles.