Best R Language MCQs 1

The post is about “MCQs R Language” which will help you to check your ability to execute some basic operations on objects in the R language and will help in understanding some basic concepts. This quiz may also improve your computational understanding, and it will also help you to learn and practice R language MCQs.

Online Multiple Choice Questions about R Language

1. _______ command is used to skip an iteration of a loop.

 
 
 
 

2. _________ and _________ are types of matrices functions?

 
 
 
 

3. Which function is used to create the vector with more than one element?

 
 
 
 

4. Packages are useful in collecting sets into a _____ unit

 
 
 

5. Vectors come in two parts _____ and _____.

 
 
 
 

6. R Language functionality is divided into a number of ________

 
 
 
 

7. R is technically much closer to the Scheme language than it is to the original _____ language.

 
 
 
 

8. The ____________ in R is a vector.

 
 
 
 

9. R is an __________ programming language?

 
 
 
 

10. In 1991, R was created by Ross Ihaka and Robert Gentleman in the Department of Statistics at the University of

 
 
 
 

11. In R Language every operation has a ______ call.

 
 
 
 

12. Who developed R?

 
 
 

13. R was named partly after the first names of ____ R authors.

 
 
 
 

14. Which of the following is a primary tool for debugging?

 
 
 
 

15. R is an interpreted language.  It can be accessed through _____________?

 
 
 
 

16. Which of the following commands will find the maximum value in the vector x, excluding the missing values

 
 
 
 

17. How many types of R objects are present in the R data type?

 
 
 
 

18. Many quantitative analysts use R as their ____ tool.

 
 
 
 

19. _________ initiates an infinite loop right from the start.

 
 
 
 

R Language MCQs with Answers

R FAQS Logo: R Language MCQs
  • R is an interpreted language.  It can be accessed through ———–?
  • Who developed R?
  • Many quantitative analysts use R as their ———– tool.
  • R is an ———– programming language?
  • R was named partly after the first names of ———– R authors.
  • Packages are useful in collecting sets into a ———– unit
  • How many types of R objects are present in the R data type?
  • Which of the following is a primary tool for debugging?
  • Which function is used to create the vector with more than one element?
  • In R Language every operation has a ———– call.
  • The ———– in R is a vector.
  • Vectors come in two parts ———– and ———–.
  • ———– and ———– are types of matrices functions?
  • Which of the following commands will find the maximum value in the vector x, excluding the missing values
  • ———– initiates an infinite loop right from the start.
  • ———– command is used to skip an iteration of a loop.
  • In 1991, R was created by Ross Ihaka and Robert Gentleman in the Department of Statistics at the University of
  • R is technically much closer to the Scheme language than it is to the original ———– language.
  • R Language functionality is divided into a number of ———–.
R Language MCQs

https://itfeature.com

https://gmstat.com

One-Way ANOVA in R

Learn how to perform One-Way ANOVA in R with code examples, post-hoc tests (Tukey HSD), assumption checks, and result interpretation. Perfect for beginners & researchers!

The two-sample t or z-test is used to compare two groups from an independent population. However, if there are more than two groups, One-Way ANOVA (analysis of variance) or its further versions can be used in R.

Introduction to One-Way ANOVA

The statistical test associated with ANOVA is the F-test (also called F-ratio). In the ANOVA procedure, an observed F-value is computed and then compared with a critical F-value derived from the relevant F-distribution. The F-value comes from a family of F-distribution defined by two numbers (the degrees of freedom). Note that the F-distribution cannot be negative as it is the ratio of variance, and variances are always positive numbers.

The One-Way ANOVA is also known as one-factor ANOVA. It is the extension of the independent two-sample test for comparing means when there are more than two groups. The data in One-Way ANOVA is organized into several groups based on grouping variables (called factor variables, too).

To compute the F-value, the ratio of “the variance between groups” and the “variance within groups” needs to be computed. The assumptions of ANOVA should also be checked before performing the test. We will learn how to perform One-Way ANOVA in R.

One-Way ANOVA in R

Suppose we are interested in finding the difference of miles per gallon based on the number of cylinders in an automobile; from the dataset “mtcars”. Let us get some basic insight into the data before performing the ANOVA.

# load and attach the data mtcars
attach(mtcars)
# see the variable names and initial observations
head(mtcars)

Let us draw the boxplot of each group

boxplot(mpg ~ cyl, main="Boxplot", xlab="Number of Cylinders", ylab="mpg")

Basic Syntax of aov() Function

# Using aov()
result <- aov(dependent_variable ~ independent_variable, data = dataset)
summary(result)

# Using lm() (alternative method)
result <- lm(dependent_variable ~ independent_variable, data = dataset)
anova(result)

Now, to perform One-Way ANOVA in R using the aov() function. The example for performing One-Way ANOVA in R is as follows

aov(mpg ~ cyl)

The variable “mpg” is continuous, and the variable “cyl” is the grouping variable. From the output note, the degrees of freedom are under the variable “cyl”. It will be one. It means the results are not correct as the degrees of freedom should be two as there are three groups on “cyl”. In the mode (data type) of grouping variable required for ANOVA  should be the factor variable. For this purpose, the “cyl” variable can be converted to a factor as

cyl <- as.factor(cyl)

Now re-issue the aov( ) function as

aov(mpg ~ cyl)

Now the results will be as required. To get the ANOVA table, use the summary() function as

summary(aov (mpg ~ cyl))

Let’s store the ANOVA results obtained from aov() function in object say res

res <- aov(mpg ~ cyl)
summary(res)

Let us find the means of each number of the cylinder group

print(model.tables(res, "means"), digits = 4)
Step-by-step One-Way ANOVA in R

Post-hoc tests for ANOVA in R (Tukey HSD)

Post-hoc tests or multiple-pairwise comparison tests help in finding out which groups differ (significantly) from one other and which do not. The post-hoc tests allow for multiple-pairwise comparisons without inflating the type-I error. To understand it, suppose the level of significance (type-I error) is 5%. Then, the probability of making at least one Type-I error (assuming independence of three events), the maximum family-wise error rate, will be

$1-(0.95 \times 0.95 \times 0.95) =  14.2%$

It will give the probability of having at least one FALSE alarm (type-I error).

To perform Tukey’s post hoc test and plot the group’s differences in means from Tukey’s test.

# Tukey Honestly Significant Differences
TukeyHSD(res)
plot(TukeyHSD(res))

Diagnostic Plots (Checking Model Assumptions)

The diagnostic plots can be used to check the assumptions of heteroscedasticity, normality, and influential observations.

layout(matrix(c(1, 2, 3, 4), 2, 2))
plot(res)
Diagnostic Plots for One-Way ANOVA in R

Levene’s Test

To check the assumption of ANOVA, Levene’s test can be used. For this purpose leveneTest() function can be used which is available in the car package.

library(car)
leveneTest(res)

https://itfeature.com

https://gmstat.com

For loop in R

In different programming languages and R, the for loop (for statement) allows one to specify the set of codes (commands) that should be repeated a fixed number of times. The for loop in R is not limited to integers or even numbers in the input. Character vectors, logical vectors, lists, or even expressions can also be used in a for loop in R.

“For loops” are fundamental programming constructs that allow you to execute a block of code. In R, for loops are particularly useful for iterative operations on vectors, lists, or data frames.

General Syntax of For Loop in R

The general syntax of the for loop in R Language is

for (named vector) {
   statements (R codes)
}

The curly braces contain a set of commands so that these commands can be treated as a single command and can be repeated the desired number of times. However, if there is only a single statement, then there is no need to use curly braces.

Let us understand the loop through different examples. Note that some of the examples can be done without the use of a for loop or with alternatives such as apply(), lapply(), sapply(), and tapply() functions.

For Loop Examples in R

Example: Suppose you want to compute the squared values for 1 to 10. Let us do this using a for loop in R, as shown below:

for (i in 1:10){
    squared <- i^2
    print(squared)
}

Note: If you write the print(squared) command outside the for loop (after the curly braces), then the last result of the loop iteration will be displayed in the console only, that is, the square of the last number (n = 10) will be printed.

To store the result of each iteration in a variable (vector, matrix, data frame, or list), a container (variable) needs to be specified with the sample length as that of the loop. For example, the outcome of each iteration (from the above example) can be stored in a variable as,

result <- vector("numeric", 10)

for (i in length(result) ){
    squared <- i^2
    result[i] <- squared
}

result

Now, results can be displayed without print() command as the results are stored in a container (vector variable). To store results in a data frame or matrix (in the form of a table) with an iteration number, the above example can be extended as

result <- data.frame(matrix(nrow = 10, ncol = 2))
colnames(result) <- c("i", "Square")

for (i in 1:10 ){
    squared <- i ^ 2
    result[i, 1] <- i  # stores iteration number in 1stcolumn of data frame
    result[i, 2] <- squared # stores iteration result in 2nd column of data frame
}

result

Nesting For Loop in R

Placing the loop inside the body of another loop is called nesting. For nested loops, the outer loop takes control of the iteration of the inner loop. The inner loop will be executed (iterated) n-times for every iteration of the outer loop. For example

# Nested For loop in R Language
for (i in 1:10){
    for (j in 1:5){
        print(i*j)
    }
}

There will be a total of 50 iterations. For each iteration of the first loop (outer loop), there will be five iterations in the inner loop.

The break statement can be used inside a loop if one wants to stop the iteration when a certain condition (situation) occurs, and the control will be out of the loop when the condition is satisfied. For example,

# break statement in for loops
n <- 1:5
for (i in x){
   if( i == 3){
     break
   }
print(i)
}

It is also possible to jump to the next iteration using the next statement when certain conditions are satisfied. For example,

# next statement in for loops
n <- 1:5

for( i in x ){
    if (i == 2){
       next
    }
print(i)
}

Now consider the example of a for loop using a character vector

# for loop in R using a character vector
v = LETTERS[1:10]
for(i in v){
    print(i)
}

Using For Loops in Simulations

In simulations, loops can be used to generate or resample (bootstrap) data. For example, let us create a variable having 1000 observations (n = 1000), where each observation is a function of the previous observation according to the equation $y_t$ is 80%, $y_{t-1} + 20%$. with random noise having a mean of 0 and a standard deviation of 1. The value of$y_1=1$.

# Simulating response variable in R using for loop
y <- rep(1,1000)

for(i in 2:1000){
    y[i] = 0.8 * y[i-1] + 0.2 * rnorm(1)
}

y

Consider another example of generating simulated data in R. Suppose you want to simulate the mixture data and want to repeat it many times, and you also want to store the data for each time.

# generating simulated data in R

n = 100
res = list()
N = 1000
X = matrix(0, nrow = N, ncol = 2)

for(i in 1:n){
    U = runif(N, min = 0, max = 1)
    for(j in 1:N){
        if (U[j] & 0.8){
          X[j,] <- rnorm(1, 2.5, 3)
        } else{
             X[j,] <- rnorm(1,2,1)
          }
        }
  
    res[[i]] = X
}

res[[100]]
Simulated Data using for loop in R

Note that each res[[i]] is a separate data set, which can be used for further calculations.

When to Avoid For Loops in R Language

In R Language, vectorized operations are often preferable:

  • Use apply() family functions (such as lapply(), sapply(), mapply() etc.)
  • Consider purrr::map() functions from the tidyverse
  • For data frames, dplyr operations are often more efficient

However, for loops remain valuable for:

  • Complex iterative algorithms
  • Cases where operations depend on previous iterations
  • When readability is more important than maximum performance

Learn about Conditional Statements in R
Online MCQs Statistics with Answers