Get Started

# Create an ordered factor

Look at the plot created over on the right. It looks great, but look at the order of the bars! No order was specified when you created the factor, so, when R tried to plot it, it just placed the levels in alphabetical order. By now, you know that there is an order to credit ratings, and your plots should reflect that!

As a reminder, the order of credit ratings from least risky to most risky is:

AAA, AA, A, BBB, BB, B, CCC, CC, C, D

To order your factor, there are two options.

When creating a factor, specify ordered = TRUE and add unique levels in order from least to greatest:

credit_rating <- c("AAA", "AA", "A", "BBB", "AA", "BBB", "A")

credit_factor_ordered <- factor(credit_rating, ordered = TRUE,
levels = c("AAA", "AA", "A", "BBB"))

For an existing unordered factor like credit_factor, use the ordered() function:

ordered(credit_factor, levels = c("AAA", "AA", "A", "BBB"))

Both ways result in:

credit_factor_ordered

[1] AAA AA  A   BBB AA  BBB A
Levels: AAA < AA < A < BBB

Notice the < specifying the order of the levels that was not there before!

This is a part of the course

## “Introduction to R for Finance”

View Course

### Exercise instructions

• The character vector credit_rating is in your workspace.
• Use the unique() function with credit_rating to print only the unique words in the character vector. These will be your levels.
• Use factor() to create an ordered factor for credit_rating and store it as credit_factor_ordered. Make sure to list the levels from least to greatest in terms of risk!
• Plot credit_factor_ordered and note the new order of the bars.

### Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Use unique() to find unique words
unique(___)

# Create an ordered factor
credit_factor_ordered <- factor(___, ordered = ___, levels = c(___))

# Plot credit_factor_ordered

This exercise is part of the course

## Introduction to R for Finance

BeginnerSkill Level
4.9+
10 reviews

Learn essential data structures such as lists and data frames and apply that knowledge directly to financial examples.

## Chapter 1: The Basics

Get comfortable with the very basics of R and learn how to use it as a calculator. Also, create your first variables in R and explore some of the base data types such as numerics and characters.

Exercise 1: Welcome to Introduction to R for Finance!Exercise 2: Your first R scriptExercise 3: Arithmetic in R (1)Exercise 4: Arithmetic in R (2)Exercise 5: Assignment and variables (1)Exercise 6: Assignment and variables (2)Exercise 7: Financial returnsExercise 8: Financial returns (1)Exercise 9: Financial returns (2)Exercise 10: Basic data typesExercise 11: Data type explorationExercise 12: What's that data type?

## Chapter 4: Factors

Questions with answers that fall into a limited number of categories can be classified as factors. In this chapter, you will use bond credit ratings to learn all about creating, ordering, and subsetting factors.

Exercise 1: What is a factor?Exercise 2: Create a factorExercise 3: Factor levelsExercise 4: Factor summaryExercise 5: Visualize your factorExercise 6: Bucketing a numeric variable into a factorExercise 7: Ordering and subsetting factorsExercise 8: Create an ordered factor
Exercise 9: Subsetting a factor

## Chapter 5: Lists

Wouldn't it be nice if there was a way to hold related vectors, matrices, or data frames together in R? In this final chapter, you will explore lists and many of their interesting features by building a small portfolio of stocks.

Exercise 1: What is a list?Exercise 2: Create a listExercise 3: Named listsExercise 4: Access elements in a listExercise 5: Adding to a listExercise 6: Removing from a listExercise 7: A few list creating functionsExercise 8: Split itExercise 9: Split-Apply-CombineExercise 10: AttributesExercise 11: Congratulations!

### What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.