When creating new columns from a data frame, sometimes you are interested in only keeping the new columns you add, and removing the ones you do not need. dplyr’s
transmute() function will add new columns while dropping the existing columns that may no longer be useful for your analysis. Let’s go back to the original inventory data frame for your store, The Handy Woman.
|1||3 inch screw||0.50||0.75|
|2||2 inch nail||0.10||0.25|
transmute() takes name-value pairs as arguments. The names will be the names of the new columns you are adding, and the values are expressions defining the values of the new columns. The difference, however, is that
transmute() returns a data frame with only the new columns.
profit columns while dropping all other columns from the data frame:
df %>% transmute(sales_tax = price * 0.075, profit = price - cost_to_manufacture)
This inventory table will now look like this:
Update the code in the last code block to add the columns
dogs while dropping all existing columns.
Inspect the new data frame with
The new columns have been added, and all the old columns have been dropped. But, wait! What
breed do the column values refer to?
breed back into the
dogs data frame by adding the following line of code as the first argument of the call to
breed = breed
This will keep the
breed column from being dropped in the transmuted data frame!