Important MCQs R Vectors Data Structure 5

This quiz “MCQs R Vectors” covers the topics related to creating different types of vectors in R, vector operations, Functions for vectors, naming, and concatenating vectors in R. Let us start with the Quiz on MCQs R Vectors.

Online MCQs R Language Test

1. ________ operator is used to create integer sequences.

 
 
 
 

2. A data analyst wants to store a sequence of data elements that all have the same data type in a single variable. What R concept allows them to do this?

 
 
 
 

3. The following command can be used to print an object “x” in R?

 
 
 
 

4. Which tidyverse package contains a set of functions, such as select(), that help with data manipulation?

 
 
 
 

5. Which of the following statements about vectors in R is correct?

 
 
 
 

6. A data analyst inputs the following code in RStudio:
sales_1 <- 100 * sales_2
Which of the following types of operators does the analyst use in the code?

 
 
 
 

7. If a command is incomplete at the end of a line, R will give a different prompt, by default it is _____

 
 
 
 

8. What is the output of the following R code?

r <- 0:10
r[2]

 
 
 
 

9. Which of the describes R Language best

 
 
 
 

10. What is the output of the following R code?
y <- 0:5
vector(y)
y[3]

 
 
 
 

11. A data analyst finds the code mdy(10211020) in an R script. What is the year of the date that is created?

 
 
 
 

12. What are ggplot2, tidyr, dplyr, and forcats all a part of?

 
 
 
 

13. Which function can you use to create a different plot for each type of cut of diamond?

 
 
 
 

14. How can one define ‘undefined value’ in R Language?

 
 
 
 

15. Which tidyverse package is used for data visualization?

 
 
 
 

16. A data analyst wants to combine values using mathematical operations. What type of operator would they use to do this?

 
 
 
 

17. What is the output of the following R code?
x <- c("a", "b")
as.numeric(x)

 
 
 
 

18. An analyst is organizing a dataset in RStudio using the following code:
arrange(filter(Storage_1, inventory >= 40), count)
Which of the following examples is a nested function in the code?

 
 
 
 

19. Identify the wrong statement:

 
 
 
 

20. How one can define ‘infinity’ in R Language?

 
 
 
 

In R Langauge, a vector data type is a fundamental data structure that represents a sequence of elements having the same data type. R Vectors can be of various types, including numeric, character, logical, and more.

MCQs R Vectors Data Structure

MCQs R Vectors

  • The following command can be used to print an object “x” in R.
  • What is the output of the following R code? r <- 0:10 r[2]
  • ________ operator is used to create integer sequences.
  • What is the output of the following R code? y <- 0:5 vector(y) y[3]
  • How can one define ‘infinity’ in R Language? How can one define ‘undefined value’ in R Language?
  • What is the output of the following R code? x <- c(“a”, “b”) as.numeric(x)
  • Identify the wrong statement:
  • If a command is incomplete at the end of a line, R will give a different prompt, by default it is _____
  • Which of the described R Language best
  • Which function can you use to create a different plot for each type of cut of diamond?
  • A data analyst inputs the following code in RStudio: sales_1 <- 100 * sales_2. Which of the following types of operators does the analyst use in the code?
  • What are ggplot2, tidyr, dplyr, and forcats all a part of?
  • Which tidyverse package is used for data visualization?
  • An analyst is organizing a dataset in RStudio using the following code: arrange(filter(Storage_1, inventory >= 40), count)
  • Which of the following examples is a nested function in the code?
  • Which tidyverse package contains a set of functions, such as select(), that help with data manipulation?
  • Which of the following statements about vectors in R is correct?
  • A data analyst finds the code mdy(10211020) in an R script. What is the year of the date that is created?
  • A data analyst wants to combine values using mathematical operations. What type of operator would they use to do this?
  • A data analyst wants to store a sequence of data elements with the same data type in a single variable. What R concept allows them to do this?

Learn Basic Statistics and Data Analysis

Reading, Creating, Accessing, and Import Data in R Language

R and Data Analysis, SPSS Data Analysis

Performing Linear Regression in R: A Quick Reference

Introduction to Performing Linear Regression in R

Regression is to build a function of independent variables (also known as predictors, regressors, explanatory variables, and features) to predict a dependent variable (also called a response, target, and regressand). Here we will focus on performing linear regression in R Language.

Linear regression is to predict response with a linear function of predictors as $$y=\beta_0+\beta_1x_1+\beta_2x_2+\cdots + \beta_kx_k,$$ where $x_1, x_2, \cdots, x_k$ are predictors and $y$ is the response to predict.

Before performing the regression analysis it will be very helpful to computer the coefficient of correlation between dependent variable and independent variable and also better to draw the scatter diagram.

Performing Linear Regression in R

Load the mtcars data, and check the data structure using str().

str(mtcars)

You have data stored in some external file such as CSV, then you can use read.csv() function to load the data in R. To learn about importing data files in R follow the link: Import Data files in R

Let us want to check the impact of weight (wt) on miles per gallon (mpg) and test the significance of the regression coefficient and other statistics to see the goodness of our fitted model

mod <- lm(mpg ~ wt, data = mtcars)
summary(mod)
Performing Linear Regression in R Estimation and Testing

Now look at the objects of results stored in mod

names(mod)

Getting Coefficients and Different Regression Statistics

Let us get the coefficients of the fitted regression model in R

mod$coef
coef(mod)

To obtain the confidence intervals of the estimated coefficients, one can use the confint()

confint(mod)

Fitted values from the regression model can be obtained by using fitted()

mod$fitted
fitted(mod)

The residuals can be obtained for the regression model using residual() function

mod$resid
resid(mod)

One can check the formula used to perform the simple/ multiple regression. It will tell you which variable is used as a response and others as explanatory variables.

formula (mod)

Graphical Representation of Relationship

To graphically visualize the relationship between variables or pairs of variables one can use plot() or pair() functions. Let us draw the scatter diagram between the dependent variable mpg and the explanatory variable wt using the plot() function.

plot(mpg ~ wt, data = mtcars)
Scatter Plot and Performing Linear Regression in R

One can add a best-fitted line to the scatter plot. For this purpose use abline() with an object having the class lm such as mod in this case

abline(mod)

There are many other functions and R packages to perform linear regression models in the R Language.

FAQS about Performing Linear Regression Models in R

  1. What is the use of abline() function in R?
  2. How a simple linear regression model can be visualized in R?
  3. How one can obtain fitted/predicted values of the simple linear regression model in R?
  4. Write a command that saves the residuals of lm() model in a variable.
  5. State the step-by-step procedure of performing linear regression in R.

To learn more about the lm() function in R

https://itfeature.com

Probability Distributions in R: A Comprehensive Tutorial

The article is a discussion about Probability Distributions in R Language.

We often make probabilistic statements when working with statistical Probability Distributions. We want to know four things:

  • The density (PDF) at a particular value,
  • The distribution (CDF) at a particular probability,
  • The quantile value corresponding to a particular probability, and
  • A random draw of values from a particular distribution.

Probability Distributions in R Language

R language has plenty of functions for obtaining density, distribution, quantile, and random numbers and variables.

Consider a random variable $X$ which is $N(\mu = 2, \sigma^2 = 16)$. We want to:

1) Calculate the value of PDF at $x=3$ (that is, the height of the curve at $x=3$)

dnorm(x = 3, mean = 2, sd = sqrt(16) ) 

dnorm(x = 3, mean = 2, sd = 4) 
dnorm(x = 3, 2, 4)

2) Calculate the value of the CDF at $x=3$ (that is, $P(X\le 3)$)

pnorm(q = 3, m = 2, sd = 4)

3) Calculate the quantile for probability 0.975

qnorm(p = 0.975, m = 2, sd = 4)

4) Generate a random sample of size $n = 10$

rnorm(n = 10, m = 2, sd = 5)

There are many probability distributions available in the R Language. I will list only a few.

Binomialdbinom( )qbinom( )pbinom( )rbinom( )
tdt( )qt( )pt( )rt( )
Poissondpois( )qpois( )ppois( )rpois( )
fdf( )qf( )pf( )rf( )
Chi-Squaredchisq( )qchisq( )pchisq( )rchisq()

Observe that a prefix (d, q, p, and r) is added for each distribution.

DistributionDistribution Name in RParameters
Binomialbinomn = Number of trials, and p= probability of success for one trial
Geometricgeomp=probability of success for one trial
Poissonpoislambda = mean
Betabetashape1, shape2
Chi-Squarechisqdf=degrees of freedom
Ffdf1, df2 degrees of freedom
Logisticlogislocation, scale
normalnormmean, sd
Student’s ttdf=degrees of freedom
Weibullweibullshape, scale

Drawing the Density Function

The density function dnorm() can be used to draw a graph of normal (or any distribution). Let us compare two normal distributions both with mean = 20, one with sd = 6, and the other with sd = 3.

For this purpose, we need $x$-axis values, such as $\overline{x} \pm 3SD \Rightarrow 20 + \pm 3\times 6$.

xaxis <- seq(0, 40, 0.5)
y1 <- dnorm(xaxis, 20, 6)
y2 <- dnorm(xaxis, 20, 3)

plot(xaxis, y2, type = "l", main = "comparing two normal distributions", col = "blue")

points(xaxis, y1, type="l", col = "red")
Comparing Normal Probability Distributions in R

Finding Probabilities in R

Probabilities in R language can be computed using pnorm() function for normal distribution.

#Left Tailed Probability
pnorm(1.96)

#Area between two Z-scores
pnorm(1.96) - pnorm(-1.96)

Finding Right-Tailed Probabilities

1 - pnorm(1.96)

Solving Real Problem

Suppose, you took a standardized test that has a mean of 500 and a standard deviation of 100. You took 720 marks (score). You are interested in the approximate percentile on this test.

To solve this problem, you have to find the Z-score of 720 and then use the pnorm( ) to find the percentile of your score.

zscore <- scale(x = 720,  500,  100)

pnorm(2.2)
pnorm(zscore[1,1])
pnorm(zscore[1])
pnorm(zscore[1, ])

MCQs in Statistics