0% found this document useful (0 votes)
2 views

r22 Unit3 Factors Dataframes

Uploaded by

227r1a67a3
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

r22 Unit3 Factors Dataframes

Uploaded by

227r1a67a3
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Factors in R

• Factors are intended to hold categorical data: variables that can take on one of
several distinct values from a set. Examples of categorical variables include gender,
state of residence and educational attainment.
• Factors take categorical data and assign each category an integer value. The
number of factor categories or "levels" is equal to the number of unique elements in
the vector used the make the factor. For example, a factor representing biological
sex would have two levels: male and female.
• You can create a factor by passing a character or numeric vector into the
factor() function.
• In this case there are no data points that take on the level "other" but the
factor allows for the possibility of encountering the category "other”
• You can check, rename and add to the levels of a factor with the levels()
function:
You can remove factor levels with no data present by recreating the factor
with the factor() function or by using the droplevels() function.
• R offers a second type of factor called an ordered factor for ordinal data. Ordinal data
is non-numeric data that has some sense of natural ordering.
• For example, a variable with the levels "very low", "low", "medium", "high", and
"very high" is not numeric but it has a natural ordering, so it can be encoded as an
ordered factor.
• To create an ordered factor, use the factor() function with the additional argument
ordered=TRUE or use the ordered() function.
• While factors can be useful for storing categorical data during analysis, they
usually not what you want if you need to clean, alter or otherwise process
the data prior to analysis. Convert a factor to character using as.character().
as.character(gender_factor)
'male' 'male' 'male' 'male' 'male' 'male' 'male' 'male' 'male' 'male' 'female'
'female' 'female' 'female' 'female' 'female' 'female' 'female' 'female' 'female'
'female' 'female' 'female' 'female' 'female’

If you try to convert a factor to numeric, the result will be a numeric vector
corresponding to the integers assigned to each factor level.
as.numeric(gender_factor)
1111111111222222222222222
• If you'd like to add more values to an existing factor, you can't just use c()
like you would when combining normal vectors. One way to add to a
factor is to convert the factor to character, concatenate it with the new
values, and then convert it back to factor.
Factor Summary Functions

You might also like