Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a factor from the vector colors.
R Programming
colors <- c("red", "blue", "green", "blue") factor_colors <- factor([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name
factor instead of the vector.Passing
levels which is not a vector.✗ Incorrect
The
factor() function converts a vector into a factor. You need to pass the vector colors as the argument.2fill in blank
mediumComplete the code to get the levels of the factor f.
R Programming
f <- factor(c("small", "medium", "large", "medium")) levels_list <- [1](f)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
class() which returns the object type, not levels.Using
length() which returns the number of elements.✗ Incorrect
The
levels() function returns the levels of a factor in R.3fill in blank
hardFix the error in the code to set the levels of factor f to "low", "medium", "high".
R Programming
f <- factor(c("medium", "low", "high")) levels(f) <- [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets which are not valid for vectors in R.
Using
list() which is not the correct type for levels.✗ Incorrect
To set levels, you must assign a character vector using
c() with all desired levels.4fill in blank
hardFill both blanks to create a factor f with levels "small", "medium", "large" and then get its levels.
R Programming
f <- factor(c("medium", "small", "large"), levels = [1]) result <- [2](f)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
factor instead of levels to get levels.Setting levels in wrong order or with wrong function.
✗ Incorrect
You set the levels explicitly with
c() and then use levels() to get them.5fill in blank
hardFill all three blanks to create a factor f from sizes, set levels to "S", "M", "L", and check if "XL" is a level.
R Programming
sizes <- c("M", "S", "L", "M") f <- factor(sizes, levels = [1]) levels_list <- [2](f) has_xl <- [3] %in% levels_list
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
factor instead of levels to get levels.Checking membership with wrong variable or string.
✗ Incorrect
Set levels with
c(), get levels with levels(), and check membership with "XL" %in% levels_list.