10.52 Rain

20180723 The two remaining character variables are: rain_today, rain_tomorrow. Their distributions are generated by dplyr::select()ing from the dataset those variables that start with rain_ and then build a base::table() over those variables. We use base::sapply() to apply base::table() to the selected columns to count the frequency of the occurrence of each value of a variable within the dataset.

# Review the distribution of observations across levels.

ds %>%
  select(starts_with("rain_")) %>%
  sapply(table)
##     rain_today rain_tomorrow
## No      208388        208393
## Yes      58928         58924

Noting that No and Yes are the only values these two variables will take it makes sense to convert them both to factors. We will keep the ordering as alphabetic and so a simple call to base::factor() will to convert from character to factor.

# Note the names of the rain variables.

ds %>%
  select(starts_with("rain_")) %>%
  names() ->
vnames

# Confirm these are currently character variables.

ds[vnames] %>% sapply(class)
##    rain_today rain_tomorrow 
##      "factor"      "factor"
# Convert these variables from character to factor.

ds[vnames] %<>%
  lapply(factor) %>%
  data.frame() %>%
  as_tibble()

# Confirm they are now factors.

ds[vnames] %>% sapply(class)
##    rain_today rain_tomorrow 
##      "factor"      "factor"


If you find this curated material useful then you can consider a donation to support it's ongoing availability and give you access to the PDF version of this book. The material has been scoped up by Generative AI without permission or any kind of recompense so do consider a donation if you can afford it. Unlike Generative AI your access to this materials is freely given. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Togaware has a 30 year tradition of making popular open source software which includes sold privacy preserving productivity apps, rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0