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:
|1||3 inch screw||0.50||0.75|
|2||2 inch nail||0.10||0.25|
You can add a new column to the data frame using the
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
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:
|1||3 inch screw||0.50||0.75||0.06|
|2||2 inch nail||0.10||0.25||0.02|
The data from the American Kennel Club on dog breed size and popularity has been loaded into a data frame
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!
Add a new column to
avg_height that is the average of
height_high_inches. Save this new data frame to
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