R Markdown Inline Code: Adding a Conjunction to Listings

In my last blog post, I wrote a couple of lines about EFFECT, a clinical trial I'm currently involved in. EFFECT is a cross-over trial with two wash-out and two study phases. After each of the four phases, the participating hospitals receive a summary of some study results. When I write these summary reports using R Markdown, I put a character string with the names of the ICUs into an inline R expression.

The character vector containing the names of the ICUs, I usually extract from one of the data frames I initially load into the document. Here, I create it manually:

names.icu <- LETTERS[1:3]

When I put this vector into an inline R expression, e.g.

  • Hospital X takes part with the following ICUs: r names.icu.

I get the following output:

“Hospital X takes part with the following ICUs: A, B, C.”

This is not exactly what I want, because by convention the last element of a listing should be preceded by a conjunction (in English: and). While some languages require the next-to-last element of a listing to be followed by a comma, other languages don't. Since the hospitals taking part at the trial have between one and five intensive care units (ICU), I needed to write a function to cover the following cases:

  • If the string vector has one element only, it must not be followed by a conjunction;
  • If the string vector has got more than one element, the next-to-last word of the listing must be followed by:
    • both comma and conjunction or
    • conjunction only.

The add_and() Function

The function I wrote can be found in the following code chunk:

add_and <- function(x, conj = "and") {
  l <- length(x)
  if (l > 1) {
    x[l] = paste(x[l - 1], conj, x[l])
    x = x[-(l - 1)]
    x = sub("\\s,", ",", x)
  }
  else {
    x
  }
  x
}

The function has got two input parameters:

  • x: the character vector and
  • conj: the conjunction (default = “and”) which may be preceded by comma and white space (“, and”) or not (“and”).

First, the function checks, whether the character vector has got more than one element. If not, it is returned as is. If yes, the conjunction is put before the last element of the vector. If the next-to-last listing element is followd by comma, the sub() function (sub("\\s,", ",", x)) removes the white space preceding this comma.

Examples

The following examples show how my function works.

Example 1: Vector with 1 element

  • Hospital X takes part with the following ICU(s): r add_and(names.icu[1]).

returns:

“Hospital X takes part with the following ICU(s): A.”

Example 2: Vector with 3 elements with no conjunction specified

  • Hospital X takes part with the following ICU(s): r add_and(names.icu).

returns:

“Hospital X takes part with the following ICU(s): A, B and C.”

Example 3: Vector with 3 elements with conjunction specified (German)

  • Krankenhaus X nimmt mit den folgenden ITS teil: r add_and(names.icu, 'und').

returns:

“Krankenhaus X nimmt mit den folgenden ITS teil: A, B und C.”

Example 4: Vector with 3 elements with conjunction preceded by comma

  • Hospital X takes part with the following ICU(s): r add_and(names.icu, ', and').

returns:

“Hospital X takes part with the following ICU(s): A, B, and C.”

Nothing New under the Sun

A former Professor of mine sometimes said that reading prevents from discovering “new” things. He was right: A couple of weeks ago, I discovered that the knitr package includes a function (combine_words()) with similar functionality. 🙂

Advertisements

About norbert

Biometrician at Clinical Trial Centre, Leipzig University (GER), with degrees in sociology (MA) and public health (MPH).
This entry was posted in Tips & Tricks and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.