Learn
Modifying Data Frames in R

Sometimes you might want to add a new column to a data frame. This new column could be a calculation based on the data that you already have.

Suppose you own a hardware store called The Handy Woman and have a data frame containing inventory information:

product_id product_description cost_to_manufacture price
1 3 inch screw 0.50 0.75
2 2 inch nail 0.10 0.25
3 hammer 3.00 5.50
4 screwdriver 2.50 3.00

You can add a new column to the data frame using the `mutate()` function. `mutate()` takes a name-value pair as an argument. The name will be the name of the new column you are adding, and the value is an expression defining the values of the new column in terms of the existing columns. `mutate()` returns a new data frame with the added column.

Maybe you want to add a column to your inventory table with the amount of sales tax that is charged for each item. The following code multiplies each `price` by `0.075`, the sales tax in your state:

``````df %>%
mutate(sales_tax = price * 0.075)``````

Now the inventory table has a column called `sales_tax`, where the value is `0.075 * price`:

product_id product_description cost_to_manufacture price sales_tax
1 3 inch screw 0.50 0.75 0.06
2 2 inch nail 0.10 0.25 0.02
3 hammer 3.00 5.50 0.41
4 screwdriver 2.50 3.00 0.22

### Instructions

1.

The data from the American Kennel Club on dog breed size and popularity has been loaded into a data frame `dogs`.

Inspect the data frame using `head()`. Make sure to click the arrows in the rendered notebook to explore each column of the data frame.

You are about to modify this data frame!

2.

Add a new column to `dogs` named `avg_height` that is the average of `height_low_inches` and `height_high_inches`. Save this new data frame to `dogs`.

Use `head()` to inspect the new data frame, and use the arrows to navigate to the last column of the data frame, which will now be `avg_height`.