In this exercise set, we will practice using the forcats factor manipulation package by Hadley Wickham. In the last exercise set, we saw that it is entirely possible to deal with factors in base R, but also that things can get a bit involved and un-intuitive. Forcats simplifies many common factor manipulation tasks and […]

## Fighting Factors With Cats: Solutions

Below are the solutions to these exercises on “Fighting Factors With Cats.” #################### # # # Exercise 1 # # # #################### library(gapminder) library(forcats) # Solution based on version: packageVersion(“forcats”) ## [1] ‘0.3.0’ packageVersion(“gapminder”) ## [1] ‘0.3.0’ gp <- gapminder fct_count(gp$continent) ## # A tibble: 5 x 2 ## f n ## <fct> <int> ## […]

## Facing The Facts About Factors: Solutions

Below are the solutions to these exercises on “Facing The Facts About Factors.” #################### # # # Exercise 1 # # # #################### library(gapminder) gp <- gapminder # How many factors? sum(sapply(gp, is.factor)) ## [1] 2 # How many levels does each have? lapply(Filter(is.factor, gp), nlevels) ## $country ## [1] 142 ## ## $continent ## […]

## Facing the Facts about Factors: Exercises

Factor variables in R can be mind-boggling. Often, you can just avoid them and use characters vectors instead – just don’t forget to set stringsAsFactors=FALSE. They are, however, very useful in some circumstances, such as statistical modelling and presenting data in graphs and tables. Relying on factors but misunderstanding them has been known to “eat […]

## Melt and Cast The Shape of Your Data-Frame: Solutions

Below are the solutions to these exercises on “Melt and Cast The Shape of Your Data-Frame.” #################### # # # Exercise 1 # # # #################### suppressMessages(library(data.table)) df <- data.frame( id = 1:2, q1 = c(“A”, “B”), q2 = c(“C”, “A”), stringsAsFactors = FALSE ) df ## id q1 q2 ## 1 1 A C […]

## Melt and Cast The Shape of Your Data-Frame: Exercises

Data-sets often arrive to us in a form that is different from what we need for our modeling or visualization functions, which, in turn, don’t necessarily require the same format. Reshaping data.frames is a step that all analysts need, but many struggle with. Practicing this meta-skill will, in the long-run, result in more time to […]

## Sharpening The Knives in The data.table Toolbox: Solutions

Below are the solutions to these exercises on “Sharpening The Knives in The data.table Toolbox.” #################### # # # Exercise 1 # # # #################### library(gapminder) library(data.table) gp <- gapminder # Set as data.table setDT(gp) gp[, uniqueN(country)] ## [1] 142 #################### # # # Exercise 2 # # # #################### gp[, gdpPercap_l1 := shift(gdpPercap), by […]

## Sharpening the Knives in the data.table Toolbox: Exercises

If knowledge is power, then knowledge of data.table is something of a super power, at least in the realm of data manipulation in R. In this exercise set, we will use some of the more obscure functions from the data.table package. The solutions will use set(), inrange(), chmatch(), uniqueN(), tstrsplit(), rowid(), shift(), copy(), address(), setnames() […]

## Programmatically Creating Text Outputs in R: Solutions

Below are the solutions to these exercises on “Pro-grammatically Creating Text Outputs in R.” #################### # # # Exercise 1 # # # #################### prices <- c( 14.3409087337707, 13.0648270623048, 3.58504267621646, 18.5077076398145, 16.8279241011882 ) sprintf(“$%.2f”, prices) ## [1] “$14.34” “$13.06” “$3.59” “$18.51” “$16.83” #################### # # # Exercise 2 # # # #################### fn <- c(25, […]

## Programmatically Creating Text Outputs in R: Exercises

In the age of Rmarkdown and Shiny, or when making any custom output from your data, you want your output to look consistent and neat. Also, when writing your output, you often want it to obtain a specific (decorative) format defined by the html or LaTeX engine. These exercises are an opportunity to refresh our […]