R MCQs

R MCQs

These R multiple-choice questions and their answers will help you strengthen your grip on the subject of R. You can prepare for an upcoming exam or job interview with these 100+ R MCQs.
So scroll down and start answering.

1: Which R function fits a simple linear regression model of x predicting y?

A.   lm(x ~ y)

B.   proc reg; model y=x; run;

C.   lm(y ~ x)

D.   regress y x

2: Suppose you need to randomly assign 100 study subjects into one of 5 groups such that there are exactly 20 subjects in each group. Which of the following R functions creates a variable Y that randomly distributes the 100 study subjects into 5 groups of equal size?

A.   ytemp <- rnorm(100, 0, 1); Y <- cut(ytemp, breaks=c(-10, -2, -1, 1, 2, 10))

B.   Y <- rbinom(100, 5, 0.5)

C.   Y <- sample(rep(c(1:5), each=20), size=100)

D.   Y <- sample(c(1, 2, 3, 4, 5), 100, replace = TRUE)

3: What is an appropriate syntax for a 'for' loop in R to run specific simulation code nnn times?

A.   for(i in 1:nnn) { <simulation code> }

B.   for i in 1:nnn: <simulation code>

C.   for(i - nnn) { <simulation code> }

D.   for(i=1, nnn, 1) { <simulation code> }

4: What does 'x:y' mean in R?

A.   It means a range from x to y.

B.   It means that x and y are correlated.

C.   It means that y logically follows from x.

D.   It means that x and y are real numbers.

E.   It means odds of x to y apply to this operation.

5: What does the following R code output? seq(2,10, by=2)

A.   2 10

B.   1 3 5 7 9

C.   2 4 6 8 10

D.   2

6: Given the command: Z <- array(h, dim=c(3,4,2)), dim(Z) stands for:

A.   the data vector as it was in h

B.   the entire array as an array

C.   the array of all zeros

D.   the dimension vector c(3,4,2)

7: What's the output for command: print("Hello World")

A.   [1] 2

B.   [1] "Hello World"

C.   NULL

D.   NA

E.   [1] Hello World

8: What is the current proper style for assigning the numeric value 1 to a variable called "a"?

A.   a = 1

B.   a == 1

C.   a <<- 1

D.   a <- 1

E.   a -> 1

9: What is the value of x after executing the command: x <- 3 < 4

A.   4

B.   3

C.   TRUE

D.   FALSE

E.   1

10: Which R function imports a comma separated file "X.csv"?

A.   import.csv("X.csv")

B.   import("X.csv")

C.   read.csv("X.csv")

D.   read("X.csv")

11: Which R code will sort the vector X <- c(1,5,3,9,7) from largest to smallest?

A.   sort(X, decreasing=T)

B.   sort(X)

C.   order(X)

D.   X[sort(X)]

12: Which R function provides basic descriptive summary statistics for a numeric vector x?

A.   summarize x

B.   summary(x)

C.   proc contents x;

D.   summarize(x)

13: Which R function provides a histogram of the numeric vector X?

A.   plot(X)

B.   hist(X)

C.   plot X

D.   plot.hist(X)

14: How does one obtain the FIRST element of x when x <- 1:9?

A.   x$1

B.   x[0]

C.   x.1

D.   x[1]

E.   x$0

15: What does the following R function output? abs(10 - 3 * 4)

A.   0

B.   -2

C.   NA

D.   2

16: The entities on which R operates are technically known as:

A.   objects

B.   mode

C.   events

D.   commands

17: Which of the following is NOT a valid data import function?

A.   readdata

B.   scan

C.   read.csv

D.   read.table

18: Which command is used to test if an object is a time series?

A.   as.ts

B.   is.ts

C.   ts

D.   if.ts

19: In order to apply lag operator for the data, you should:

A.   convert the data into integer

B.   convert the data into vector

C.   convert the data into matrices

D.   convert the data into the time-series object

20: What is the default value for missing data that is read into R via read.table or read.csv?

A.   NA

B.   NaN

C.   #VALUE!

D.   [a blank cell]

E.   NULL

21: What does the option 'cex' do in the following R function? text(0, 1, "Hello", cex=2)

A.   Surpresses printing of the word "Hello"

B.   Right justifies the word "Hello"

C.   Prints the word "Hello" in bold font

D.   Increases the size of the word "Hello" by a factor of 2

22: In the plot( ) function, what option is used to specify that the x-axis displays values from 0 to 1?

A.   xlim=c(0, 1)

B.   xlimit="0, 1"

C.   xlim="0, 1"

D.   xlab=c(0, 1)

23: What is the output of the following R code? x<-2; y <- c(1:3); paste("The value of x is", x, "and the value of y[x] is", y[x], sep=" ")

A.   "The value of x is 2 and the value of y[x] is c(1:3)[2]"

B.   "The value of x is 2 and the value of y[x] is 4"

C.   "The value of x is 2 and the value of y[x] is 2"

D.   "The value of x is 2 and the value of y[x] is 6"

24: What's the output for command: seq(from = 1, to = 5, by = 3)

A.   1 4

B.   1 4 7

C.   1 3 5

D.   1 2 3 4 5

25: Which R function adds a line with slope 1 and intercept 0 into an existing plot of Y versus X?

A.   lines(0, 1)

B.   lty=c(0, 1)

C.   abline(0, 1)

D.   smooth.spline(0, 1)

26: What is the output of this command: > is.na( c( 1:3, NA ) )

A.   [1] FALSE FALSE FALSE TRUE

B.   [1] FALSE

C.   Throws an error.

D.   [1] TRUE

E.   [1] NA NA NA TRUE

27: Suppose we define X <- NA. What is the output for > is.nan( X ); is.na( X )

A.   [1] TRUE [1] FALSE

B.   [1] TRUE [1] TRUE

C.   [1] FALSE [1] TRUE

D.   NULL

E.   [1] NaN [1] NA

28: What is the value of Y? X <- c(1,2,2,2,3,3,4,4,5,6); Y <- unique(X[which(X<4)])

A.   3

B.   1 2 3 4

C.   1 2 2 2 3 3

D.   1 2 3

29: Which function is used to load a CSV file into R?

A.   read.table()

B.   load.table()

C.   load()

D.   read()

30: What R function can be used to tabulate values of categorical variable Y (columns) by categorical variable X (rows)?

A.   tabulate(X, Y)

B.   table(Y ~ X)

C.   table(X*Y)

D.   table(X, Y)

31: What is the output for the command: is.na(0/0)?

A.   NA

B.   TRUE

C.   FALSE

D.   -Inf

32: What does the following R code output? Y <- c(1,2,3); X <- rep(c(1,length(Y)), times=2); X

A.   1 2 1 2

B.   1 1

C.   1 1 3 3

D.   1 3 1 3

33: What is the output of the following R function? yyy <- c(1, 3, NA); fff <- function(xxx) {mean(xxx)}; fff(yyy)

A.   NULL

B.   2

C.   1.5

D.   NA

34: What is the output of dim(matrix(1:10, ncol=2))?

A.   5 2

B.   10

C.   2 5

D.   20

35: In a linear regression model with outcome y and linear predictors x1, x2, and x3, which R code correctly includes an interaction term between x1 and x2?

A.   lm(y ~ x1:x3 + x1:x2)

B.   lm(y ~ x1 + x2 + x3 + x1*x2)

C.   lm(y ~ x1 + x2 + x3 + interaction(x1, x2))

D.   lm(y ~ x1:x2 + x3)

36: Which of the following is NOT included in CRAN's R distribution?

A.   Time Series Analysis

B.   Generalized Linear Models

C.   Linear Mixed Effects Models

D.   Analysis of Variance Models

E.   Clustering Tools

37: In order to assess whether x and y are equal, you can use the command:

A.   x == y

B.   x != y

C.   x =! y

D.   x =~ y

E.   x %% y

38: Which R function will generate a mean for each row in a numeric matrix X with intermittent missing values?

A.   mean(X[1:length(X[,1]),], na.rm=T)

B.   apply(X, 1, mean, na.rm=T)

C.   by(X, 1, mean, na.rm=T)

D.   apply(X, 1, mean)

39: Suppose numeric vectors X, Y, and Z are all of the same length. Which R command will create a matrix with rows X, Y, Z?

A.   matrix(X, Y, Z, nrow=3)

B.   matrix(cbind(X, Y, Z), nrow=3)

C.   matrix(rbind(X, Y, Z), nrow=3)

D.   matrix(rbind(X, Y, Z), ncol=3)

40: Which R command is used to merge two data sets X and Y by the variable "ID" that includes all records from both data sets?

A.   merge( X, Y, by = "ID")

B.   merge( X, Y, by = "ID", all = TRUE )

C.   merge( X, Y, sort = "ID", all = TRUE)

D.   combine( X, Y, by = "ID")

41: Which object class can contain multiple unrelated object classes of potentially different sizes?

A.   matrix

B.   vector

C.   data.frame

D.   list

E.   array

42: I want to use the sort() function, and I see in the help file that I my sort "an R object with a class, or a numeric, complex, character, or logical vector." Given this guidance, which is a valid call to sort()?

A.   sort( data.frame( x = c(10, -3, 4) ) )

B.   sort( list( 10, -3, 4) )

C.   sort( c( 10, -3, 4) )

D.   sort( 10, -3, 4 )

43: Which is NOT a valid R function for obtaining the residuals from a simple linear regression model of X predicting Y?

A.   lm(Y ~ X)$residuals

B.   lm(Y ~ X)$residuals - lm(Y ~ X)$fitted.values

C.   residuals(lm(Y ~ X))

D.   Y - lm(Y - X)$fitted.values

44: Let X be a 3x4 matrix with non-zero values. The result of: > apply(X, 2, mean), is:

A.   equal to the result of: >c(mean(X[,1]), mean(X[,2]), mean(X[,3]))

B.   equal to the result of: >c(mean(X[1,]), mean(X[2,]), mean(X[3,]), mean(X[4,]))

C.   equal to the result of: >c(mean(X[,1]), mean(X[,2]), mean(X[,3]), mean(X[,4]))

D.   equal to the result of: >c(mean(X[1,]), mean(X[2,]), mean(X[3,]))

45: Suppose X is the vector c(1:10). Which R code will swap the values of the 3rd element of X and the 7th element of X?

A.   X[c(3,7)] <- X[c(7,3)]

B.   replace(X, c(3, 7), c(7,3)

C.   X[7] <- X[3]; X[3] <- X[7]

D.   X[3] <- X[7]; X[7] <- X[3]

46: What is the output for the command: > is.na( NaN ) ?

A.   [1] NULL

B.   [1] TRUE

C.   [1] -Inf

D.   [1] NaN

E.   [1] FALSE

47: What does the following R function output? xxx <- c(1, 1, 2, 2, 3, 3, NA, NA); yyy <- c(rep(0, 4), rep(1, 4)); cor(yyy, xxx)

A.   An error message

B.   NA

C.   A correlation coefficient

D.   R squared

48: Which R function can be used to produce a simple scatterplot of a numeric vector Y versus a numeric vector X?

A.   scatter( X ~ Y )

B.   plot( data.frame( Y ~ X ) )

C.   plot( X ~ Y )

D.   xyplot( X, Y )

49: What is the value of Y? Y <- 10 + 100 & !is.na(0)

A.   True

B.   0

C.   False

D.   110

50: Given the command: Z <- array(h, dim=c(3,4,2)), Z[] with an empty subscript or Z with no subscript stands for:

A.   the dimension vector c(3,4,2)

B.   the entire array as an array

C.   the array of all zeros

D.   the data vector as it was in h