Creating data frames
Feature engineering
Summary

Instruction

Okay! So far we have updated whole columns in one go. Very often you need to update columns only for selected rows. Let's see an example. Suppose you need to change the "Terraced" value in the type column from the houses data frame to the "Terraced house":

houses[houses$type == "Terraced", "type"] <- "Terraced house"

This expression uses a row filtering syntax we already know. We first find the rows with the "Terraced" value under the type colum. Then we select the type column, and assign the new value to our result.

Note that there is a second way of filtering columns:

houses[houses$type == "Terraced",]$type <- "Terraced house"

We moved the column name that we want to update outside the square brackets.

You should remember that you can update values using a condition only for columns that already exist in the data frame. You can't add new columns to a data frame this way.

Exercise

The city council has decided to change the name of the Millford district. Change the "Millford" value from the district column in the houses data frame to "Millford Area".