0
0
R Programmingprogramming~10 mins

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

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a factor from a character vector.

R Programming
colors <- c("red", "blue", "green", "red")
factor_colors <- factor([1])
print(factor_colors)
Drag options to blanks, or click blank then click option'
Ac("red", "blue")
Bas.factor
Ccolors
Dlevels
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name as.factor instead of the vector.
Passing only part of the vector like c("red", "blue").
Using levels which is not a vector.
2fill in blank
medium

Complete the code to get the levels of a factor.

R Programming
colors <- factor(c("red", "blue", "green", "red"))
levels_list <- [1](colors)
print(levels_list)
Drag options to blanks, or click blank then click option'
Alevels
Bunique
Cas.character
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using unique() which works on vectors but not specifically for factor levels.
Using as.character() which converts factor to string but does not list levels.
Using class() which returns the type, not levels.
3fill in blank
hard

Fix the error in the code to correctly create a factor with specified levels.

R Programming
colors <- c("red", "blue", "green", "red")
factor_colors <- factor(colors, levels = [1])
print(factor_colors)
Drag options to blanks, or click blank then click option'
Ac("yellow", "blue", "green")
Bc("red", "blue", "green")
Cc("red", "green")
Dc("red", "blue")
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting some colors from the levels causes NA values.
Including colors not present in the vector is allowed but not necessary here.
Using incomplete levels causes unexpected factor behavior.
4fill in blank
hard

Fill both blanks to create a factor and check if it is a factor.

R Programming
data <- c("small", "medium", "large", "medium")
f <- [1](data)
is_fact <- [2](f)
print(is_fact)
Drag options to blanks, or click blank then click option'
Afactor
Bis.factor
Cas.factor
Dis.character
Attempts:
3 left
💡 Hint
Common Mistakes
Using as.factor is also valid to create a factor but here factor is expected.
Using is.character returns FALSE because the object is a factor, not a character vector.
5fill in blank
hard

Fill all three blanks to create a factor with ordered levels and check if it is ordered.

R Programming
sizes <- c("small", "medium", "large", "medium")
ordered_sizes <- factor(sizes, levels = [1], ordered = [2])
is_ordered <- [3](ordered_sizes)
print(is_ordered)
Drag options to blanks, or click blank then click option'
Ac("small", "medium", "large")
BTRUE
Cis.ordered
DFALSE
Attempts:
3 left
💡 Hint
Common Mistakes
Using unordered levels or ordered = FALSE will make the factor unordered.
Using is.factor() instead of is.ordered() will not check ordering.