library(SomaDataIO) library(dplyr) knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
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:
dplyr::rename()
function via !!!
syntaxAlternatively, roll-your-own rename()
function
Note: all entries in the mapping (i.e. key) object must be
present as names
in the data frame object.
mtcars
# Create map/key of the names to map key <- c(MPG = "mpg", CARB = "carb") # named vector key # rename `mtcars` rename(mtcars, !!! key) |> head()
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)
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.