We start by changing the name of the BostonHousing data to simply data for ease of use. Independent variables that are of the factor type are then converted to the numeric type using the lapply function.
Note that for this data, the only factor variable is chas; however, for any other dataset with more factor variables, this code will work fine.
Take a look at the following code:
# Converting factor variables to numeric
data <- BostonHousing
data %>% lapply(function(x) as.numeric(as.character(x)))
data <- data.frame(data)
In the preceding code, after converting factor variables to the numeric type, we also change the format of data to data.frame.