A new platform is looking for Alpha testers. Sign up, it's free!

# First things 1st

July 3, 2020

We like creating content with R Markdown (like this entire site!). 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)

lanetimeplace
110.719763
210.884915
311.779359
411.035256
511.064647
611.8575310
711.230468
810.367471
910.656572
1010.777174

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"

Or create a free DataKwery.com account

### Related Courses

DataCamp
Introduction to Natural Language Processing in R
Kasey Jones

4 hours

Beginner

\$300

3,625

### Related Learning Paths

Coursera
Johns Hopkins University

5 Courses 7 Months