Replacing missing values with a constant

Replacing all NA values with a constant is actually rather simple. A dataframe can be indexed using another dataframe of logical values of the same dimension. The following will create a new dataframe that is a copy of roads3 and replace all of the NA values with 0:

roads.replace.na <- roads3
roads.replace.na[is.na(roads3)] <- 0

In this chapter, I won't use the dataframe with replaced NA values which was just created, so this is just for demonstration purposes. A more effective way to handle NA values, when possible, is to replace the missing value with an estimate based on existing data. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset