library(SomaDataIO)
library(dplyr)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Introduction

Renaming variables/features of a data frame (or tibble) is a common task in data science. Doing so safely is often a struggle. This can be achieved safely via the dplyr::rename() function via 2 steps:

  1. Set up the mapping in either a named vector
  2. Apply the dplyr::rename() function via !!! syntax
  3. Alternatively, roll-your-own rename() function

  4. Note: all entries in the mapping (i.e. key) object must be present as names in the data frame object.

Example with mtcars

# Create map/key of the names to map
key <- c(MPG = "mpg", CARB = "carb")   # named vector
key

# rename `mtcars`
rename(mtcars, !!! key) |> head()

A SomaScan example (example_data)

Occasionally it might be required to rename AptNames (seq.1234.56) -> SeqIds (1234-56) when analyzing SomaScan data.

getAnalytes(example_data) |> 
  head()

# create map (named vector)
key2 <- getAnalytes(example_data)  
names(key2) <- getSeqId(key2)     # re-name `seq.XXXX` -> SeqIds
key2 <- c(key2, ID = "SampleId")  # SampleId -> ID
head(key2, 10L)

# rename analytes of `example_data`
getAnalytes(example_data) |>
  head(10L)

new <- rename(example_data, !!! key2)

getAnalytes(new) |>
  head(10L)

Alternative to dplyr

If you prefer to avoid the dplyr import/dependency, you can achieve a similar result with similar syntax by writing your own renaming function:

rename2 <- function (.data, ...) {
  map <- c(...)
  loc <- setNames(match(map, names(.data), nomatch = 0L), names(map))
  loc <- loc[loc > 0L]
  newnames <- names(.data)
  newnames[loc] <- names(loc)
  setNames(.data, newnames)
}

Now, with similar syntax (but cannot use !!!):

# rename `mtcars` in-line
rename2(mtcars, MPG = "mpg", CARB = "carb") |>
  head()

# rename `mtcars` via named `key`
rename2(mtcars, key) |>
  head()


SomaLogic/SomaDataIO documentation built on Feb. 8, 2025, 12:19 p.m.