Bird
Raised Fist0
R Programmingprogramming~20 mins

Why factors represent categorical data in R Programming - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Factor Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this factor levels code?
Consider this R code that creates a factor variable. What will be the output of levels(fruit)?
R Programming
fruit <- factor(c("apple", "banana", "apple", "orange"))
levels(fruit)
A[1] "apple" "banana" "orange"
B[1] "apple" "orange"
C[1] "banana" "orange"
D[1] "apple" "banana"
Attempts:
2 left
💡 Hint
Think about what unique categories the factor stores.
🧠 Conceptual
intermediate
2:00remaining
Why are factors used for categorical data in R?
Which of the following best explains why factors represent categorical data in R?
AFactors automatically sort data in alphabetical order without labels.
BFactors store only numeric data and cannot hold text values.
CFactors convert all data to character strings for display.
DFactors store data as integers with labels, making categories easy to manage and analyze.
Attempts:
2 left
💡 Hint
Think about how factors internally represent categories.
❓ Predict Output
advanced
2:00remaining
What happens when you compare two factors with different levels?
What will be the output of this R code?
R Programming
f1 <- factor(c("low", "medium", "high"), levels = c("low", "medium", "high"))
f2 <- factor(c("low", "medium", "high"), levels = c("high", "low", "medium"))
f1 == f2
A[1] FALSE FALSE FALSE
B[1] TRUE TRUE TRUE
CError: factors with different levels cannot be compared
D[1] TRUE FALSE TRUE
Attempts:
2 left
💡 Hint
Check how factor levels affect comparison.
🔧 Debug
advanced
2:00remaining
Why does this factor conversion produce unexpected results?
What is the output of this code and why?
R Programming
x <- c("10", "2", "30")
f <- factor(x)
as.numeric(f)
A[1] 10 2 30
B[1] 1 2 3
CError: cannot convert factor to numeric directly
D[1] NA NA NA
Attempts:
2 left
💡 Hint
Remember how factors store data internally.
🚀 Application
expert
2:00remaining
How many unique categories does this factor have after modification?
Given this R code, what is the number of unique categories in f after the assignment?
R Programming
f <- factor(c("red", "blue", "green"))
levels(f) <- c("red", "blue", "yellow")
f <- c(f, "yellow")
length(levels(f))
A5
B4
CError: invalid factor level
D3
Attempts:
2 left
💡 Hint
Think about how levels are changed and what happens when adding a new value.

Practice

(1/5)
1. Why are factors used in R to represent categorical data?
easy
A. Because factors automatically sort data alphabetically
B. Because factors store only numeric data
C. Because factors group data into categories with levels
D. Because factors convert data into dates

Solution

  1. Step 1: Understand what factors do in R

    Factors group data into distinct categories called levels, which represent categorical data.
  2. Step 2: Compare with other options

    Factors do not store only numeric data, nor do they convert data into dates or automatically sort alphabetically.
  3. Final Answer:

    Because factors group data into categories with levels -> Option C
  4. Quick Check:

    Factors = Categories [OK]
Hint: Factors group data into categories, not just numbers or text [OK]
Common Mistakes:
  • Thinking factors only store numbers
  • Assuming factors sort data automatically
  • Confusing factors with date conversion
2. Which of the following is the correct way to create a factor in R from a character vector colors <- c('red', 'blue', 'red')?
easy
A. factor(colors)
B. as.numeric(colors)
C. sort(colors)
D. levels(colors)

Solution

  1. Step 1: Identify the function to create a factor

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

    as.numeric() converts to numbers, sort() sorts the vector, and levels() retrieves levels but does not create a factor.
  3. Final Answer:

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

    Create factor with factor() [OK]
Hint: Use factor() to convert vectors to categorical data [OK]
Common Mistakes:
  • Using as.numeric() instead of factor()
  • Trying to sort instead of factor conversion
  • Confusing levels() as a creation function
3. What will be the output of the following R code?
colors <- c('red', 'blue', 'red')
f <- factor(colors)
levels(f)
medium
A. ["red", "blue"]
B. ["blue", "red"]
C. ["red"]
D. ["blue"]

Solution

  1. Step 1: Create factor and check levels

    The factor f is created from colors which has values 'red' and 'blue'. Levels are sorted alphabetically by default.
  2. Step 2: Determine levels order

    Levels will be ["blue", "red"] because R sorts factor levels alphabetically unless specified otherwise.
  3. Final Answer:

    ["blue", "red"] -> Option B
  4. Quick Check:

    Factor levels sorted alphabetically [OK]
Hint: Factor levels are alphabetical by default [OK]
Common Mistakes:
  • Assuming levels keep original order
  • Confusing factor values with levels
  • Expecting only one level
4. Identify the error in this R code that tries to create an ordered factor:
sizes <- c('small', 'medium', 'large')
f <- factor(sizes, ordered=TRUE)
levels(f) <- c('small', 'medium', 'large')
medium
A. Levels must be set before creating the factor
B. Ordered factor cannot have character levels
C. levels() assignment should use factor() argument, not after
D. No error, code is correct

Solution

  1. Step 1: Trace the code execution

    factor(sizes, ordered=TRUE) creates an ordered factor with default alphabetical levels. Then levels(f) <- c('small', 'medium', 'large') reassigns levels in the desired order, which is valid.
  2. Step 2: Verify no errors

    Assigning levels after creation works for ordered factors and sets the order as listed. The code runs without error.
  3. Final Answer:

    No error, code is correct -> Option D
  4. Quick Check:

    levels(f) <- works on ordered factors [OK]
Hint: levels() <- assignment works after factor creation [OK]
Common Mistakes:
  • Thinking levels must only be set inside factor()
  • Believing ordered factors cannot have character levels
  • Assuming levels assignment fails after creation
5. You have a vector responses <- c('low', 'high', 'medium', 'low'). How do factors help when analyzing this data as categorical with a natural order?
hard
A. Factors allow defining levels with order: low < medium < high
B. Factors convert all responses to numeric values automatically
C. Factors remove duplicate values from the vector
D. Factors sort the vector alphabetically by default

Solution

  1. Step 1: Understand natural order in categorical data

    Responses like 'low', 'medium', 'high' have a natural order that factors can represent by setting ordered levels.
  2. Step 2: How factors help analysis

    By creating an ordered factor with levels c('low', 'medium', 'high'), R can compare and sort these categories meaningfully.
  3. Final Answer:

    Factors allow defining levels with order: low < medium < high -> Option A
  4. Quick Check:

    Ordered factors represent natural category order [OK]
Hint: Use ordered factors to keep category sequence [OK]
Common Mistakes:
  • Assuming factors convert to numbers automatically
  • Thinking factors remove duplicates
  • Confusing alphabetical sorting with natural order
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