Bird
Raised Fist0
R Programmingprogramming~20 mins

Factor creation in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Factor Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of factor levels with default ordering
What is the output of the following R code?
f <- factor(c("apple", "banana", "apple", "cherry"))
levels(f)
R Programming
f <- factor(c("apple", "banana", "apple", "cherry"))
levels(f)
A[1] "cherry" "banana" "apple"
B[1] "banana" "apple" "cherry"
C[1] "apple" "cherry" "banana"
D[1] "apple" "banana" "cherry"
Attempts:
2 left
💡 Hint
Think about how R orders factor levels by default.
❓ Predict Output
intermediate
2:00remaining
Number of levels in a factor
What is the output of this R code?
f <- factor(c("red", "blue", "red", "green", "blue"))
length(levels(f))
R Programming
f <- factor(c("red", "blue", "red", "green", "blue"))
length(levels(f))
A1
B3
C5
D4
Attempts:
2 left
💡 Hint
Count unique values in the vector.
❓ Predict Output
advanced
2:00remaining
Effect of specifying levels in factor creation
What is the output of this R code?
f <- factor(c("small", "medium", "large", "medium"), levels = c("large", "medium", "small"))
levels(f)
R Programming
f <- factor(c("small", "medium", "large", "medium"), levels = c("large", "medium", "small"))
levels(f)
A[1] "large" "medium" "small"
B[1] "small" "medium" "large"
C[1] "medium" "small" "large"
D[1] "large" "small" "medium"
Attempts:
2 left
💡 Hint
Levels are set explicitly in the factor function.
❓ Predict Output
advanced
2:00remaining
Output of factor with unused levels
What is the output of this R code?
f <- factor(c("cat", "dog"), levels = c("cat", "dog", "mouse"))
length(levels(f))
R Programming
f <- factor(c("cat", "dog"), levels = c("cat", "dog", "mouse"))
length(levels(f))
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Levels include all specified, even if not used in data.
🧠 Conceptual
expert
2:30remaining
Behavior of factor when converting numeric vector
Consider this R code:
v <- c(10, 20, 10, 30)
f <- factor(v)
levels(f)

What is the output of levels(f)?
R Programming
v <- c(10, 20, 10, 30)
f <- factor(v)
levels(f)
A[1] "1" "2" "3"
B[1] 10 20 30
C[1] "10" "20" "30"
D[1] 10 20 30 40
Attempts:
2 left
💡 Hint
Factor levels are always character strings representing unique values.

Practice

(1/5)
1. What is the main purpose of creating a factor in R?
easy
A. To represent categorical data with specific levels
B. To perform mathematical calculations faster
C. To store large numeric datasets efficiently
D. To create complex graphical plots automatically

Solution

  1. Step 1: Understand factor usage in R

    Factors are used to represent categorical data with fixed categories called levels.
  2. Step 2: Compare options with factor purpose

    Only To represent categorical data with specific levels correctly describes factors as categorical data with levels.
  3. Final Answer:

    To represent categorical data with specific levels -> Option A
  4. Quick Check:

    Factor = categorical data with levels [OK]
Hint: Factors are for categories, not numbers or plots [OK]
Common Mistakes:
  • Thinking factors speed up math calculations
  • Confusing factors with numeric vectors
  • Assuming factors create plots automatically
2. Which of the following is the correct way to create a factor from a character vector colors <- c('red', 'blue', 'red')?
easy
A. matrix(colors)
B. factor(colors)
C. list(colors)
D. as.numeric(colors)

Solution

  1. Step 1: Identify function to create factor

    The function factor() converts a vector into a factor with levels.
  2. Step 2: Check other options

    as.numeric() converts to numbers, list() and matrix() create different structures, not factors.
  3. Final Answer:

    factor(colors) -> Option B
  4. Quick Check:

    Use factor() to create factors [OK]
Hint: Use factor() to convert vectors to factors [OK]
Common Mistakes:
  • Using as.numeric() instead of factor()
  • Confusing list() or matrix() with factor creation
  • Not wrapping vector inside factor()
3. What will be the output of the following R code?
colors <- c('red', 'blue', 'red')
f <- factor(colors, levels = c('blue', 'red'))
levels(f)
medium
A. [1] "red" "blue"
B. [1] "red"
C. [1] "blue" "red"
D. [1] "blue"

Solution

  1. Step 1: Understand factor levels argument

    The levels argument sets the order and categories explicitly as c('blue', 'red').
  2. Step 2: Check levels(f) output

    Calling levels(f) returns the levels in the order specified: "blue", then "red".
  3. Final Answer:

    [1] "blue" "red" -> Option C
  4. Quick Check:

    levels() shows levels in given order [OK]
Hint: levels() shows factor levels in specified order [OK]
Common Mistakes:
  • Assuming levels are sorted alphabetically by default
  • Confusing factor values with levels
  • Ignoring the levels argument order
4. Identify the error in this R code snippet:
colors <- c('red', 'blue', 'green')
f <- factor(colors, levels = c('red', 'blue'))
summary(f)
medium
A. factor() cannot take levels argument
B. colors vector must be numeric for factor()
C. summary() cannot be used on factors
D. The levels argument is missing 'green', causing NA values

Solution

  1. Step 1: Check levels vs data values

    The vector has 'green' but levels only include 'red' and 'blue'. This causes 'green' to become NA in the factor.
  2. Step 2: Understand summary() behavior

    summary() counts factor levels including NAs, so missing 'green' in levels causes unexpected NA counts.
  3. Final Answer:

    The levels argument is missing 'green', causing NA values -> Option D
  4. Quick Check:

    Levels must include all categories to avoid NA [OK]
Hint: Include all categories in levels to avoid NA [OK]
Common Mistakes:
  • Thinking factor() ignores missing levels
  • Believing summary() can't handle factors
  • Assuming colors must be numeric
5. You have a character vector responses <- c('yes', 'no', 'yes', 'maybe', 'no'). You want to create an ordered factor with levels no < maybe < yes. Which code correctly creates this ordered factor?
hard
A. factor(responses, levels = c('no', 'maybe', 'yes'), ordered = TRUE)
B. factor(responses, levels = c('yes', 'no', 'maybe'), ordered = TRUE)
C. factor(responses, ordered = TRUE)
D. factor(responses, levels = c('no', 'maybe', 'yes'))

Solution

  1. Step 1: Set levels in desired order

    Levels must be set as c('no', 'maybe', 'yes') to reflect the order no < maybe < yes.
  2. Step 2: Make factor ordered

    Use ordered = TRUE to create an ordered factor reflecting this order.
  3. Final Answer:

    factor(responses, levels = c('no', 'maybe', 'yes'), ordered = TRUE) -> Option A
  4. Quick Check:

    Ordered factor needs levels and ordered=TRUE [OK]
Hint: Set levels in order and use ordered=TRUE [OK]
Common Mistakes:
  • Not setting ordered=TRUE for ordered factors
  • Incorrect levels order
  • Omitting levels argument
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard