R Programming Quizzes

Factors

Question 1

What is the primary purpose of the factor data structure in R?

Question 2

Consider the following R code: colors <- factor(c("Red", "Blue", "Red", "Green")). What will the function levels(colors) return?

Question 3

The unique, predefined values that a factor can take are known as its _____.

Your answer here.

Question 4

You have a vector of T-shirt sizes and want to create an ordered factor where 'Small' < 'Medium' < 'Large'. Select the correct R code to achieve this.

Question 5

Which of the following statements about R factors are true?

Question 6

Given the factor f <- factor(c("100", "50", "100")), what is the result of as.numeric(f)?

Question 7

The function used to find the number of unique levels in a factor is _____().

Your answer here.

Question 8

Which of these functions can be used to check if an R object is a factor?

End of Quiz

Your final score is: 0 / 0

List of Questions

Question 1: What is the primary purpose of the `factor` data structure in R?

Answers: To store and represent categorical data with predefined levels.

Question 2: Consider the following R code: `colors <- factor(c("Red", "Blue", "Red", "Green"))`. What will the function `levels(colors)` return?

Answers: c("Blue", "Green", "Red").

Question 3: You have a vector of T-shirt sizes and want to create an ordered factor where 'Small' < 'Medium' < 'Large'. Select the correct R code to achieve this.

Answers: factor(sizes, levels = c("Small", "Medium", "Large"), ordered = TRUE).

Question 4: Which of the following statements about R factors are true?

Answers: Internally, factors are stored as integers., Factors can help save memory when you have a character vector with many repeated values., You can convert a factor to a numeric vector using `as.numeric(as.character(factor_variable))`..

Question 5: Given the factor `f <- factor(c("100", "50", "100"))`, what is the result of `as.numeric(f)`?

Answers: c(2, 1, 2).

Question 6: Which of these functions can be used to check if an R object is a factor?

Answers: is.factor().