---
jupyter:
  jupytext:
    text_representation:
      extension: .Rmd
      format_name: rmarkdown
      format_version: '1.1'
      jupytext_version: 1.1.1
  kernelspec:
    display_name: Python 3
    language: python
    name: python3
---

```{python nbsphinx=hidden}
import pandas as pd
pd.set_option("display.max_rows", 5)
```

## Mutate

```{python}
from siuba import _, group_by, mutate, select
from siuba.data import mtcars

small_cars = mtcars[["mpg", "cyl", "hp"]]
```

### Assign new column

```{python}
mutate(small_cars, cyl2 = _.cyl * 2)
```

```{python}
mutate(small_cars, cyl2 = _.cyl * 2, cyl4 = _.cyl2 * 2)
```

```{python}
mutate(small_cars, misc = "hey")
```

### Used with group_by

```{python}
(small_cars
  >> group_by(_.cyl)
  >> mutate(
       hp_mean = _.hp.mean(),
       demeaned_hp = _.hp - _.hp_mean
     )
  )
```

```{python}
(small_cars
  >> group_by(_.cyl)
  >> mutate(
       hp_per_cyl = _.hp / _.cyl,
       diff = _.hp_per_cyl - _.hp_per_cyl.shift(1)
     )
  )
```

### With if_else and case_when

TODO

```{python}

```
