Five useful R functions

2023-11-25

A selection of five useful R functions that make my life a little easier and my code more robust and readable:

1 file.path()

When writing a file path in R I can never remember if I need to use ‘\’ or ‘/’ on Windows/Mac/Linux. But I don’t need to know, because I can use file.path() to construct a file path from one or more character vectors and the correct separator will be used. For example:

data_dir <- file.path("data", "imported")
file.path(data_dir, "raw")
## [1] "data/imported/raw"
file.path("output", "plots")
## [1] "output/plots"

2 here::here()

I typically use project-oriented workflows and relative file paths in my scripts. But sometimes I need a full file path (e.g. when knitting an R markdown document in a subdirectory of a project). The here package solves this for me, creating a full path when needed without having to put full file paths in my script (so it will still work when the files are moved or directories are renamed).

3 reformulate()

In the past, to construct a formula from different parts (such as in a function) I might make lots of use of paste()/paste0() and as.formula:

x <- "bmi"
y <- "sbp"
adjust <- c("age", "sex")
as.formula(paste(y, "~", x, "+", paste(adjust, collapse = " + ")))
## sbp ~ bmi + age + sex

But then I found reformulate() which makes creating a formula from character vectors much easier (and neater):

reformulate(c(x, adjust), "sbp")
## sbp ~ bmi + age + sex

4 glue::glue()

The glue package provides functions for creating strings. I now find myself using it instead of paste in many cases. Often I find the code is more readable, especially for more complex strings where I want to include the results of multiple R expressions. A simple example:

name <- "Neil"
glue::glue('My name is {name}, and my favourite number is {1+2+3}.')
## My name is Neil, and my favourite number is 6.

5 broom::tidy()

After fitting a model I want the results in a nice, tidy data frame. The functions provided by the broom package help do exactly that.

fit <- lm(dist ~ speed, cars)
broom::tidy(fit)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)   -17.6      6.76      -2.60 1.23e- 2
## 2 speed           3.93     0.416      9.46 1.49e-12