First things 1st

We like creating content with R Markdown. It makes it easy to write one-off posts like these and also opens the door for automated reporting. The challenge is crafting dynamic text that sounds natural.

One simple tool to help comes from the toOrdinal package in R. It turns 5 into 5th and 2 into 2nd to make describing relative position data a breeze.

Simple example

Imagine we have data from ten runners showing the time is took in seconds to complete a 100-meter race. We can add rank for where they finished with min_rank() from tidyverse/dplyr.

library(tidyverse)
library(knitr)
set.seed(123)

runners <- 10
results <- data.frame(lane  = 1:runners,
                      time = rnorm(runners, 11, .5))

results <- results %>% mutate(place = min_rank(time))

kable(results)
lane time place
1 10.71976 3
2 10.88491 5
3 11.77935 9
4 11.03525 6
5 11.06464 7
6 11.85753 10
7 11.23046 8
8 10.36747 1
9 10.65657 2
10 10.77717 4

Say you want to write the runner in lane x completed the race in y seconds, finishing in z place. Focusing on the runner in lane four, we could write:

one_runner <- results %>% filter(lane == 4)

my_text <- str_c("The runner in lane ", 
      one_runner$lane, 
      " completed the race in ", 
      round(one_runner$time,2), # rounding the result for display purposes
      " seconds, finishing in ",
      one_runner$place,
      " place.")

The runner in lane 4 completed the race in 11.04 seconds, finishing in 6 place.

With the toOrdinal() function from the package with the same name, we swap out the awkward sounding “6 place” with the more natural “6th place”. This is an especially helpful transformation as it isn’t easy to write clear text rules for all possible integer values.

library(toOrdinal)

my_text_clean <- str_c("The runner in lane ", 
      one_runner$lane, 
      " completed the race in ", 
      round(one_runner$time,2),
      " seconds, finishing in ",
      toOrdinal(one_runner$place), # added the toOrdinal function around the place variable
      " place.")

The runner in lane 4 completed the race in 11.04 seconds, finishing in 6th place.

Super simple to use. Give it a try.

toOrdinal(1)
## [1] "1st"
toOrdinal(23)
## [1] "23rd"
toOrdinal(147)
## [1] "147th"
Alex
Alex
Co-founder at DataKwery

Alex loves helping people with applied analytics!

comments powered by Disqus

Related