0
0
R Programmingprogramming~5 mins

Factor creation in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a factor in R?
A factor in R is a data structure used to represent categorical data. It stores the data as integer codes with corresponding labels called levels.
Click to reveal answer
beginner
How do you create a factor from a character vector in R?
Use the function factor() and pass the character vector as an argument. For example: factor(c("apple", "banana", "apple")).
Click to reveal answer
intermediate
What does the levels argument do in factor()?
The levels argument sets the possible categories (levels) of the factor. It controls the order and which categories are included, even if not present in the data.
Click to reveal answer
beginner
How can you check the levels of a factor in R?
Use the levels() function on the factor variable. It returns a character vector of the factor's levels.
Click to reveal answer
intermediate
What happens if you create a factor with numeric values?
R treats the numeric values as categories, not numbers. The factor stores them as levels, so arithmetic operations won't work directly on factors.
Click to reveal answer
Which function is used to create a factor in R?
Afactor()
Bas.factor()
Clevels()
Dcategorize()
What does the levels argument in factor() control?
AThe order and categories of the factor
BThe numeric values of the factor
CThe length of the vector
DThe data type of the vector
If you create a factor from the vector c("red", "blue", "red"), how many levels will it have?
A1
B3
C2
D0
What will levels(factor(c(1, 2, 1))) return?
AAn error
B[1, 2]
C["1", "2", "3"]
D["1", "2"]
Can you perform arithmetic directly on factors?
AYes, factors behave like numbers
BNo, factors represent categories, not numbers
COnly if levels are numeric
DOnly with the <code>levels</code> argument
Explain how to create a factor in R and why factors are useful.
Think about how R stores categories and why that helps with data analysis.
You got /4 concepts.
    Describe what happens when you create a factor from numeric values and how it differs from numeric vectors.
    Consider how factors treat numbers as labels, not quantities.
    You got /4 concepts.