In order to make it easy and fun the use of fixedincome spot rate curve, a few operators have been implemented.

The fixedincome package is loaded and one curve `crv`

is created.

```
library(fixedincome)
<- spotratecurve(
crv c(0.1315, 0.1319, 0.1338, 0.1348, 0.1372, 0.1381),
c(1, 21, 42, 63, 126, 252),
"discrete", "business/252", "Brazil/ANBIMA",
refdate = Sys.Date()
)
```

The spot rate curve `crv`

is used in the examples.

The operator `[`

indexes the elements by their positions and returns one SpotRateCurve object.

The code below returns one SpotRateCurve object with the first, third and fifth elements.

```
c(1, 3, 5)]
crv[#> SpotRateCurve
#> 1 day 0.1315
#> 42 days 0.1338
#> 126 days 0.1372
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

This curve has the terms 1, 42, and 126 days.

The positional indexing also works with `logical`

vectors. For example, let’s get the part of the curve that is greater than 13.5%

```
> 0.135]
crv[crv #> SpotRateCurve
#> 126 days 0.1372
#> 252 days 0.1381
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

or the elements which terms are even.

```
@terms %% 2) == 0]
crv[(crv#> SpotRateCurve
#> 42 days 0.1338
#> 126 days 0.1372
#> 252 days 0.1381
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

The operator `[[`

indexes the elements according to the terms of the term structured and returns one SpotRateCurve object.

The code below returns one SpotRateCurve object with the terms 1, 21, and 42.

```
c(1, 21, 42)]]
crv[[#> SpotRateCurve
#> 1 day 0.1315
#> 21 days 0.1319
#> 42 days 0.1338
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

All these terms are present in the SpotRateCurve. If any given term doesn’t correspond to the SpotRateCurve terms, `NA`

is returned for the unmatched elements.

```
c(1, 13, 42)]]
crv[[#> SpotRateCurve
#> 1 day 0.1315
#> 13 days NA
#> 42 days 0.1338
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

To solve that an interpolation has to be set and in these cases the interpolation fulfills these gaps.

```
interpolation(crv) <- interp_linear()
c(1, 13, 42)]]
crv[[#> SpotRateCurve
#> 1 day 0.1315
#> 13 days 0.1317
#> 42 days 0.1338
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

Let’s get back to the original curve removing the interpolation.

`interpolation(crv) <- NULL`

The spot rate elements in an SpotRateCurve object can be replaced positionally with the operator `[`

.

For example, the second element can be set accordingly.

```
2] <- 0.14
crv[c(1, 2, 3)]
crv[#> SpotRateCurve
#> 1 day 0.1315
#> 21 days 0.1400
#> 42 days 0.1338
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

Negative indexes remove elements

```
-2]
crv[#> SpotRateCurve
#> 1 day 0.1315
#> 42 days 0.1338
#> 63 days 0.1348
#> 126 days 0.1372
#> 252 days 0.1381
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

But it doesn’t change the object in place, instead returns a new object without the removed elements.

```
crv#> SpotRateCurve
#> 1 day 0.1315
#> 21 days 0.1400
#> 42 days 0.1338
#> 63 days 0.1348
#> 126 days 0.1372
#> 252 days 0.1381
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

The second element (term 21) hasn’t been removed.

The spot rate elements in an SpotRateCurve object can be replaced by term with the operator `[[`

.

For example, let’s replace the element related to the term 21.

```
21]] <- 0.1320
crv[[c(1, 21, 42)]]
crv[[#> SpotRateCurve
#> 1 day 0.1315
#> 21 days 0.1320
#> 42 days 0.1338
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

Negative terms also remove elements.

```
-21]]
crv[[#> SpotRateCurve
#> 1 day 0.1315
#> 42 days 0.1338
#> 63 days 0.1348
#> 126 days 0.1372
#> 252 days 0.1381
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

Nonetheless, if the interpolation is defined, negative terms raises an error.

```
interpolation(crv) <- interp_linear()
-21]]
crv[[#> Error in validObject(.Object): invalid class "SpotRateCurve" object: FALSE
```

The methods `first`

and `last`

are handy to slice parts of the SpotRateCurve.

`first`

and `last`

return spot rate curve according to the term given in the second argument.

For example, the following call returns the elements that lies inside the first 50 days.

```
::first(crv, "50 days")
fixedincome#> SpotRateCurve
#> 1 day 0.1315
#> 21 days 0.1320
#> 42 days 0.1338
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

The `fixedincome::`

prefix is necessary to avoid conflicts with dplyr’s methods.

We can do similar with the `last`

method, for example, let’s get the elements in the last 6 months of the spot rate curve.

```
::last(crv, "6 months")
fixedincome#> SpotRateCurve
#> 126 days 0.1372
#> 252 days 0.1381
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```

There is one last `spotratecurve`

method that isn’t properly an indexing method, in the usual way, but certainly it is useful. This is the `fixedincome::closest`

method, for the `spotratecurve`

, it returns the element of the curve that is close to the given term. For example, to get the curve element that is the closest to 1 year, we do

```
::closest(crv, "1 year")
fixedincome#> SpotRateCurve
#> 252 days 0.1381
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-10-13
```