Data Science Desktop Survival Guide
by Graham Williams |
|||||
Logical |
20180723 Above we converted rain_today and rain_tomorrow to factors. They have just two values as we confirm here, in addition to a small number of missing values (NA).
ds %>%
select(rain_today, rain_tomorrow) %>% summary()
As binary valued factors, and particularly as the values suggest, they are both candidates for being considered as logical variables (sometimes called Boolean). They can be treated as FALSE/TRUE instead of No/Yes and so supported directly by R as class logical. Different functions will then treat them as appropriate but not all functions do anything special. If this suits our purposes then the following can be used to perform the conversion to logical.
|
ds %<>%
mutate(rain_today = rain_today == "Yes", rain_tomorrow = rain_tomorrow == "Yes")
Best to now check that the distribution itself has not changed.
|
ds %>%
select(rain_today, rain_tomorrow) %>% summary()
Observe that the TRUE (Yes) values are much less frequent than the FALSE (No) values, and we also note the missing values. The majority of days not having rain can be cross checked with the rainfall variable. In the previous summary of its distribution we note that rainfall has a median of zero, consistent with fewer days of actual rain. As data scientists we perform various cross checks on the hunt for oddities in the data. As data scientists we will also want to understand why there are missing values. Is it simply some rare failures to capture the observation, or for example is there a particular location not recording rainfall? We would explore that now before moving on. For our purposes going forward we will retain these two variables as factors. One reason for doing so is that we will illustrate missing value imputation using randomForest::na.roughfix() and this functoin does not handle logical data but keeping rain_tomorrow as character will allow missing value imputation. Of course we could skip this variable for the imputation.
|