0
0
R Programmingprogramming~10 mins

Factor levels in R Programming - Interactive Code Practice

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 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'
Alevels
Bc
Cfactor
Dcolors
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name factor instead of the vector.
Passing levels which is not a vector.
2fill in blank
medium

Complete 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'
Afactor
Blevels
Cclass
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using class() which returns the object type, not levels.
Using length() which returns the number of elements.
3fill in blank
hard

Fix 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'
Ac("low", "medium", "high")
B["low", "medium", "high"]
Clist("low", "medium", "high")
Dc('low', 'medium')
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.
4fill in blank
hard

Fill 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'
Ac("small", "medium", "large")
Blevels
Cfactor
Dc("large", "medium", "small")
Attempts:
3 left
💡 Hint
Common Mistakes
Using factor instead of levels to get levels.
Setting levels in wrong order or with wrong function.
5fill in blank
hard

Fill 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'
Ac("S", "M", "L")
Blevels
C"XL"
Dfactor
Attempts:
3 left
💡 Hint
Common Mistakes
Using factor instead of levels to get levels.
Checking membership with wrong variable or string.