# 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”

### 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

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 2: Vectors and Matrices

In this chapter, you will learn all about vectors and matrices using historical stock prices for companies like Apple and IBM. You will then be able to feel confident creating, naming, manipulating, and selecting from vectors and matrices.

Exercise 1: What is a vector?Exercise 2: c()ombineExercise 3: Coerce itExercise 4: Vector names()Exercise 5: Visualize your vectorExercise 6: Vector manipulationExercise 7: Weighted average (1)Exercise 8: Weighted average (2)Exercise 9: Weighted average (3)Exercise 10: Vector subsettingExercise 11: Matrix - a 2D vectorExercise 12: Create a matrix!Exercise 13: Matrix <- bind vectorsExercise 14: Visualize your matrixExercise 15: cor()relationExercise 16: Matrix subsetting## Chapter 3: Data Frames

Arguably the most important data structure in R, the data frame is what most of your data will take the form of. It combines the structure of a matrix with the flexibility of having different types of data in each column.

Exercise 1: What is a data frame?Exercise 2: Create your first data.frame()Exercise 3: What goes in a data frame?Exercise 4: Making head()s and tail()s of your data with some str()uctureExercise 5: Naming your columns / rowsExercise 6: Data frame manipulationExercise 7: Accessing and subsetting data frames (1)Exercise 8: Accessing and subsetting data frames (2)Exercise 9: Accessing and subsetting data frames (3)Exercise 10: Adding new columnsExercise 11: Present valueExercise 12: Present value of projected cash flows (1)Exercise 13: Present value of projected cash flows (2)## 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 factorExercise 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.